The return Statement
At some point a function will be finished, ready to return control to its caller. This happens, for example, when execution reaches the end of the function's block of code. Execution then picks up directly after the point where the function was called. Another way to stop execution of the function is to use the return statement.
You may have multiple return statements in your function, though you have to consider how this reduces the readability of your code. Multiple return statements can be a barrier to understanding the flow of execution. Ideally functions should have one way in and one way out. In practice there are cases when multiple return statements are acceptable.
If you follow return with an expression, the value ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access