Are there good reasons why it's a better practice to have only one return statement in a function?
Yes, there are:
- The single exit point gives an excellent place to assert your post-conditions.
- Being able to put a debugger breakpoint on the one return at the end of the function is often useful.
- Fewer returns means less complexity. Linear code is generally simpler to understand.
- If trying to simplify a function to a single return causes complexity, then that's incentive to refactor to smaller, more general, easier-to-understand functions.
- If you're in a language without destructors or if you don't use RAII, then a single return reduces the number of places you have to clean up.
- Some languages require a single exit point (e.g., Pascal and Eiffel).
The question is often posed as a false dichotomy between multiple returns or deeply nested if statements. There's almost always a third solution which is very linear (no deep nesting) with only a single exit point.
Update: Apparently MISRA guidelines promote single exit, too.
To be clear, I'm not saying it's always wrong to have multiple returns. But given otherwise equivalent solutions, there are lots of good reasons to prefer the one with a single return.