return
terminates function whenever and wherever it is called.
Consider this code block -
|
|
return
will return the control to caller - no matter what state the function is in.
You can also use conditional return, or have multiple returns in different blocks.
|
|
There was a time in history when I used to write code for a single exit point. The flow of control would be smooth and seamless from entry to exit.
Almost anyone could understand why we are exiting out of the function, at what point, and also sprinkle the exit with custom logic (e.g. set another variable whenever exiting out of function).
|
|
But, I quite don’t care about the ‘single exit point’ thing anymore.
- It looks like almost all developers are happy with
return
popping its head now and then at multiple places in the code. - Also,
returns
are not that commonly used anywhere and everywhere within a function. It was turning out to be not that efficient logic and a chore to handle logic and ensure a single return.
So, I did the only thing that any self-respecting developer will do at this point. I fell in line.