Having multiple return statements in a function is perfectly acceptable. In fact, having multiple return statements in a function like the one listed above can provide performance and readability improvements. For example, you don't need the else
block in the above function because you return from the function if the condition is met.
Just make sure that if your function does not have a return type of void
, then you have a return statement at the end, and that all return statements return that type. For example, if your function is declared like this:
int foo ();
Then all your return statements must return integers, and you should return an integer at the end, no matter what. But if your function has a return type of void, like this:
void foo ();
Then if you reach the end of the function and have no return
keyword, the function will return automatically.