Reputation: 9733
A function returns TRUE on failure and FALSE on success.
I see some such functions do this towards end of itself:
return return_code != 0;
or
return (return_code != 0);
And in this function, at each error case, it returns TRUE - which is fine and what it should do in case of error.
But what does above code signify? Is it trying to make sure that return_code is FALSE - explicitly?
Upvotes: 3
Views: 232
Reputation: 12321
A simple example to make it clear, with a function that divides a with b. Returns true if the division can be evaluated false otherwise
bool div(double a, double b, double& r)
{
int return_code = 1;
if (b == 0) // cannot divide
return_code = 0;
else
r = a/b;
return (return_code != 0);
}
In this simple example only if b==0 the return_code will be 0 so as Marc replied it will return false. In any other case it will return true. Of course there is no reason to do something like this in such a simple function. In more complicated function where the success or not can change in many places, it is a common practice to use such return statements.
Upvotes: 1
Reputation: 7203
Just a further explanaion -
It is most likely that somewhere in the program TRUE and FALSE are defined:
#define TRUE 1
#define FALSE 0
When a boolean expression is evaluated, the result is always 1 or 0, 1 if the boolean expression evaluates to true, and 0 if it is false. That is why you can test if a boolean expression evaluates to TRUE or FALSE. If you're just curious about boolean expressions and want more basic information, here is a decent tutorial.
Upvotes: 1
Reputation: 360572
Both depend on the value of return_code:
return_code = 0;
return(return_code != 0); // returns false
return(return_code == 0); // returns true
and
return_code = "anything but a zero";
return(return_code != 0); // returns true
return(return_code == 0); // returns false
Upvotes: 2
Reputation: 81349
I would say is trying to collapse from all the possible integer values to just those of 0
and 1
. I'm assuming that the function returns an integral type; evaluating the result as a boolean expression forces the result to just those two values.
Upvotes: 6