Functions

How do you exit a function prematurely?


Either enclose the rest of the function in an if block to skip over it, or use a return.

If I have a function that doesn't actually return anything, I've got into the habit of making its type boolean; put in a return (true) at the end and return (false) if it exits prematurely. Just in case a circumstance comes up later where I want to know which happened.

If the function does have a return value, then it often makes sense to return it as soon as you know it. If you want to break out of a function without knowing the return value (for example, if the function fails for some reason), then you could return null or "" or -1 depending on the type of the function. An out-of-band value that the code calling the function can check for.


This topic is now closed. Topics are closed after 60 days of inactivity.

Support

Forums