In the context of this exercise, what actually happens when returning multiple values in a function?
Answer
In Python, when returning multiple values separated by commas, all the values listed are actually wrapped inside a data type known as a tuple, which looks like (1, 2, 3). That tuple is returned, containing each value.
This is also useful because you can do what is known as “tuple unpacking” – if a tuple has multiple values, you can assign the same number of variables to the number of elements in the tuple, so that each variable is assigned to each value in the order they appear in the tuple.
Example
# This is a simple tuple unpacking.
# Each variable is assigned to each
# corresponding value.
arg1, arg2, arg3 = (1, 2, 3)
# This is a function that returns multiple values.
def times_ten(a, b, c):
a = a * 10
b = b * 10
c = c * 10
return a, b, c # This will return a tuple containing the values listed.
# Each variable will be assigned to each value in the returned tuple,
# in the order they appear.
new_a, new_b, new_c = times_ten(5, 6, 7)
# new_a = 50
# new_b = 60
# new_c = 70
Doesn’t this already assign (and store!) values to variables x_2 and y_2 as soon as the square_point function is called with concrete values of x and y?
What then is the added benefit of return x_2, y_2 in the third line of the function?
@ibagusa, I believe that the return command allows the programmer to use the returned values later on in the code, outside of the function. It is true that the example assigns a value to x_2 and to y_2 as soon as the function is called, but those values can’t be used outside of that function unless the function “returns” them. I’m sure my explanation is awkward and imprecise, but I hope it’s accurate.
I’m having a hard time understanding how, in this scenario the “return” knew to add the values to low, high without inclusion in the function or a process of defining them.
But the function stops at return. Which means, the variables low_limit and high_limit become empty. Then how are values assigned on print call? If the values of these two variables are now empty after function ends, how are they able to take the values after function stops?
The exact reason is called variable scope. Scope refers to the namespace for which a variable exists. In the BASIC programming language there was no concept of scope, and this complicated programs in a way you cannot imagine.
In Python and many other languages the default scope for all variables is local scope, meaning the level within the program where you define a variable is where that variable exists and no place else. This is not entirely correct 100% of the time, but for now you may understand the idea here.
Therefore, variables that are created inside the function definition, are local to the function and only that function. There lifetime is limited too, in the sense that they go away after the function returns/ends. This is important because as a programmer, you will probably want to use the same name for a variable in many different functions that you write that serve a similar purposes. In other words, you might always use the variable name rtn or ret or r when returning the result of a mathematical function for the sake of consistency, so when you read your programs/functions years from now you will already know what rtn, ret, or r could mean.
If variables did not have scope, and persisted forever, then all variable names would have to be unique throughout your entire program which would make coming up with useful names much more complicated. Bugs would be very difficult to find, because you would never be able to know which statement modified which variable and when.
With local scoped variables, you never need to think about or worry about this and you can be very consistent throughout all your different functions.
So, the reason you need to use the “return” statement is to make sure the correct local variables are passed back to the calling function. This helps with good coding practices too. Be careful where you put your return statements too; in other words, you should only have one per function and not 14 for example.
You’ll need to provide a little more information to know what you tried to know what didn’t work. Have a look at this FAQ for a good way to frame your question (you can ignore the bits about creating a new post for now).
At a guess you tried something like the following?
return low_limit high_limit
Which is not valid syntax. return will only return one object. As per the first post this is often a tuple which is created by using the two names low_limit, high_limit separated by the comma.
Or something with two returns?
return low_limit
return high_limit
When a return statement is executed the function itself is exited. No code grouped to this function will be executed following that return statement which includes other return statements. Which is why multiple returns are often sent back as a sequence.
*The def “defined function” is like a big warehouse.
*The equations and stuff underneath the “def” function is the workers and assembly lines making a product.
*The “return” at the end of the “def” function is like “FedEx” shipping out the finished product made from the “def” function. But the product’s destination is to another “function call” written below the “def” function.