I would have written that like so because the else
is redundant:
if (a < b) {
return 0; // bail, since condition is met
}
// lots of calculations in this block
return 1;
I don't think there's a rule of thumb, but if the function is really long and you have multiple return points, it can be hard to maintain and understand.
However, in a recursive function, for example, it's very handy to put your "base cases" as return statements at the beginning of a function.
For example, consider factorial:
int fact(int x) {
// base cases
if (x == 0 || x == 1)
return 1;
// recursive call
return x * fact(x-1);
}
You could also write it like this:
int fact(int x) {
int ret = 0;
if (x == 0 || x == 1)
ret = 1;
else
ret = x * fact(x-1);
return ret;
}
I just like the first way better, but it doesn't mean either way is better than the other.
It really comes down to whatever standards you must follow and personal preference.