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
(or similar data structure) and return that.
Java
private Array javaFunction() { String[] myArray = new String[2]; myArray[0] = "Bob"; myArray[1] = "Jim"; return myArray; }
C#
private string[] cSharpFunction() { string[] myArray = new string["Bob", "Jim"]; return myArray; }
Actionscript 3
private function actionscriptFunction():Array { var myArray:Array = ["Bob", "Jim"]; }
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.
JavaScript
function javascriptFunction () { var myArray = ["Bob", "Jim"] return myArray }
Python
def pythonFunction (): myList = ["Bob", "Jim"]; return myList;
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! :)