0

Consider the following code:

#include <iostream>
int test(int a){
    if (a > 10){
        return a;
    }
    else{
        std::cout << "Error!";
        return nothing;
    }
}
int main(){
    std::cout << test(9);
    return 0;
}

What I want is that The integer function test(int a), return a if a > 10, otherwise return Error!. but since this is an integer function, it must return an integer value, but I want that it print Error and return nothing. Is there a way for do this? (Also note that I don't want to use a void function)

Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
  • 2
    There is no such thing as `nothing` in C++. Either change the return type to `void` or return something like `0` or whatever `int` value you choose/want. One alternative is to use exceptions and `throw`. Also, see [In a non-void function I want to return nothing](https://stackoverflow.com/questions/64517100/in-a-non-void-function-i-want-to-return-nothing) – Jason Aug 15 '22 at 09:43
  • 2
    You can't return nothing. Perhaps you should return 0 or a negative number to indicate there was an error. Of course you'd have to change the code where you call it to handle that error as well. In general I wouldn't have any output in the function and instead have the caller decide whether to print the value or "Error!". – Retired Ninja Aug 15 '22 at 09:43
  • 2
    Consider introducing [exceptions](https://en.cppreference.com/w/cpp/error/exception). – rawrex Aug 15 '22 at 09:44
  • 3
    @amin have a look at `std::optional` – πάντα ῥεῖ Aug 15 '22 at 09:47
  • Why not just make the function returning a boolean indicating succcess/failure and maybe an out-parameter (or inout-parameter). This has been done for ages. Most io-function work like this. It very readable and self-explaning. – engf-010 Aug 15 '22 at 10:25

1 Answers1

-1
#include <stdexcept>

int test(int a){
    if (a > 10){
        return a;
    }
        else{
            throw std::invalid_argument( "a is smaller or eq than 10" );
    
        }

}
moldovean
  • 3,132
  • 33
  • 36
  • 1
    This is not valid C++. You missed the if branch. – Jason Aug 15 '22 at 09:48
  • 3
    A good answer should not just provide code (especially if it's just a few lines without context), but explain why and how it solves the problem at hand. OP wanted to _"print Error and return nothing"_ - that's not what your code does. You should at least explain, why you would prefer exceptions. – Lukas-T Aug 15 '22 at 09:51
  • 1
    @churill.. if you trow something that's it. You cannot return after you throw. try it and u will see your code would not even compile. – moldovean Aug 15 '22 at 09:53
  • 2
    @moldovean Well ... that's my point. OP want's to "return nothing" (which is possible with `std::optional`), but you suggest exceptions. This decision should be explained, I think. – Lukas-T Aug 15 '22 at 09:57
  • 1
    " try it and u will see your code would not even compile" of course it would comile – 463035818_is_not_an_ai Aug 15 '22 at 10:00
  • @Churill ... mmm.. yes, one could theoretically solve this thing through an optional. indeed. That would also be a valid answer. Optional is a lil bit more advanced. because something else would have to read it and evaluate it. this lines however, `std::cout << "Error!"; return nothing;' made me think Amin wanted a quick solution to terminating a program with an error – moldovean Aug 15 '22 at 10:01