Let’s think about the function signature in different (statically typed) languages. Remember, the compile breaks out of the function when return method is called, and nothing after it is run. The function defines a return type, and to a naive programmer looking at the signature it seems that one can only return a single thing. And it’s true- you can only return one thing. But, that thing can be an Object or an Array.
To return multiple values from a method, put the values in an array or object
Here’s how a function returning an Array might look in various languages:
Even in languages where you don’t specify the return type, the method still ends as soon as you call the return statement, and you are only able to return a sinlge object.
Yep, just return an array or an object! Some languages will have other cool data structures like Vector, Point, etc. You can also make custom object and return them. Oh yeah!
Sorry to anyone who feels like they got cheeseballed because they were expecting a deep, complicated programming lesson and read a post about something totally obvious. I’m just making sure I don’t forget it! :)
The posts on this site are written and maintained by Jim Lynch. About Jim...