Version 1:
int abc(...)
{
if(a || b || c))
return 1;
else
return 0;
}
Version 2:
int abc(...)
{
if(a || b || c))
return 1;
return 0;
}
Is there any difference? Which code is the proper one?
Version 1:
int abc(...)
{
if(a || b || c))
return 1;
else
return 0;
}
Version 2:
int abc(...)
{
if(a || b || c))
return 1;
return 0;
}
Is there any difference? Which code is the proper one?
There is no difference since, upon returning 1
, no further code in the function is executed anyway, and one of the paths must be chosen.
I prefer the latter since it involves less typing but that's really just a matter of preference.
Of course, there are other options as well such as the simpler:
return a || b || c;
"Is there any difference?"
No.
"Which code is the proper one?"
None. They are equivalent. Note if the else
isn´t explicitly needed for the context, omit it since it is redundant:
Unnecessary 'else' after 'return'. (No-else-return)
Note: The linked question is for Javascript, but it shares the same concern of yours.
Furthermore, your code could be simplified:
int abc (...)
{
return (a || b || c);
}
If the condition is true 1
, else 0
is returned.
Both code variants are the same and will generate same assembly instructions.
But more elegant way is:
int abc(...)
{
return (a || b || c);
}