Multiple Return Values

It's possible to return multiple values from a Function. To achieve this, we list the values, separated with a comma, on the last line of the function body, like this:

1 function f():
2 1, 2, 3

The function

f
returns the values
1
,
2
and
3
.

In order to access the returned values, we give them names during assignment, like this:

1 function f():
2 1, 2, 3
3
4 a, b, c = f() # a is 1, b is 2, c is 3
5
6 a, b = f() # a is 1, b is 2 - you don't have to use all returned values
7
8 a = f() # a is 1 - this works fine, too

We're not limited to returning simple values - expressions are allowed as well:

1 function MultAndDiv(x, y):
2 x * y, x / y
3
4 m, d = MultAndDiv(10, 5) # m is 50, d is 2

Keep in mind that if a function which returns multiple values is used in an expression, only the first return value comes into play:

1 function MultAndDiv(x, y):
2 x * y, x / y
3
4 m, d = MultAndDiv(10, 5) + 1
5 # m is 51, d is unknown
6 # MultAndDiv was used in an expression
7 # The first return value is used in the sum
8 # That sum becomes the final result of the expression