Multiple Return Values
00:09 I’ll be following Marius’s tutorial a little more closely for a while, so feel free to follow along as well if that’s what you’d like to do, I’ll still, however, do my examples in C++ instead of C#.
00:40 You saw how by using pass by reference, you can avoid the issue of memory duplication and the time involved in copying a complex object or structure from an argument variable to a parameter variable.
00:54 But Python does almost the same thing. When you use an object as an argument, the parameter name is bound to that same object. Python doesn’t make a copy of the object, even for the simplest data types, so that benefit is already provided by Python’s pass by assignment mechanism.
01:37 So what they would usually do is use the actual return value for one of them and pass another variable argument by reference and let the function give it a value, which is then retained and can be used once the function ends.
01:53 As an example, consider a program that’s to create a personalized greeting and update a count of how many times it’s been used. Here’s a version in C++. It takes one parameter, the name, to personalize the greeting, and then a reference parameter to update the count of how many times the function has been called.
It is passed as the second argument in the function call. First in a function call that’s going to create a greeting for
"Alice", which is then printed using C++’s
cout statement. And then in one for Bob, it also displays the value of
count after each greeting.
As you can see, the function
greet() takes two parameters, increments the counter, and returns the personalized greeting. The script creates a counter variable called
count, prints the greeting personalized for Alice, passing
count as the second parameter, displaying the value of
count, then doing the same for Bob. Let’s take a look at this when it runs.
Because the statement
counter += 1 is an assignment statement, Python computes the value of
counter using its current value plus
1, creates a new object with that computed value, then assigns—or rather, binds—that new object with the parameter variable
The parameter variable no longer has any connection with the original object it was bound to. The variable
count in the script is still bound to that object with a value of
0 and was unaffected by the parameter
counter being bound to a new object. Again, this is pass by assignment. By assignment,
counter was bound to the same object that
count was bound to, but that was only until it was changed to be bound to something else. Its connection, or reference, to that original object was gone.
05:47 Of course, you don’t have to do this in Python because Python allows you to return multiple values. So you can, in a single return statement, return the personalized greeting and the incremented value of how many times the function has been called packed in a tuple.
I see the updated value of
counter is being returned, but the variable
count isn’t being updated to use it. Also, printing the return value of this function is now displaying the entire tuple and not just the greeting that we had before.
The greeting is saved to the variable
greeting and the updated counter value is saved back to the variable
count. After displaying that information, the script then calls
greet() a second time, this time for
count variable is passed to that function call, and again, the return tuple is unpacked, saving the new greeting to
greeting, and saving the updated counter value back to
count. By the way, the only significance about saving the second greeting back to the same
greeting variable is because the script no longer needs the greeting for
"Alice". It’s already used it.
08:35 Assigning return values to variables is the best way to achieve the same results as passing by reference in Python. You’ll see this again later. But next, you’ll look at how pass by reference can work with functions that return different things based on certain conditions within the function and how Python can be used to do that as well.
Become a Member to join the conversation.