Returning several values from a single function call: BEHIND THE SCENES!

Hi! Welcome to this new session where we will learn HOW TO RETURN SEVERAL VALUES FROM A FUNCTION! Let’s get started!


What we’ve learned so far is that we can return a value from a function to the scope that called the function and then save it to a variable for later use.

But… what if we want to return more than one value?



This is where our newly discovered friends come into play, TUPLES AND LISTS!

If we return a tuple or a list from a function, we can return several values and assign them individually to their corresponding variables (We will see this in detail in the next diagrams). 



We’ve learned that we can display the values returned by a function using print().

Since lists and tuples are iterables, we can iterate over their elements using a for/in loop like the examples below. We can also access each individual element by its corresponding index.


But… wait a minute! What if we want to store the values returned by the function call? Can we apply the principles we’ve learned? Yes, you can. But there is a new syntax that will allow you to store each element in the tuple into its corresponding variable.

Let’s see why we need this…

In this example, we are using the function defined above, that returns a tuple with the length of each argument. In this case, (2, 5, 3).

To store them in separate variables, we would first need to create a new variable to store the entire tuple and then index individually and assign them to their variable. This is one way to achieve this but…


Keep reading to find out how you can do this in ONE LINE!



To assign the variables individually in ONE LINE:

-        On the left hand side we create as many variables as the number of values contained in the tuple returned by the function call. (In this case, 3 variables for a tuple that contains 3 values)

-        On the right hand side we call the function


You can see this process broken down into steps with a “Behind the scenes” look of what happens when the value is returned in the diagram below. 

Let’s check these values in Python’s shell. As you can see, the values are assigned individually and you can now use these variables later in your code. Yes! J




TIPS: You can convert the arguments you pass into a function into a tuple by adding an asterisk before the function’s parameter. This will convert all the arguments you pass in separated by commas into a single tuple that you can use inside your function. 



Hope this helps!

If you have any questions, please do not hesitate to post them in forums or right below this post. Community TAs and your fellow classmates will be there to help you :)

Estefania.

Comments

  1. It may be possible to return, from a Function, more than one value even without specifically forming a Tuple. In the example, given in the above post, the function 'returnStringLengths(string1, string2, string3)' the last statement may also read

    return len1, len2, len3

    ReplyDelete
    Replies
    1. Hi Rathindra, thank you very much for your comment : )

      That is right, you can use this syntax as well and it is very interesting to notice that this is really returning a tuple without you explicitly declaring it.

      If we try using this syntax:

      >>> def returnValues(x, y):
      return x, y

      >>> var1 = returnValues(3, 4)
      >>> type(var1)

      >>> isinstance(var1, tuple)
      True

      If we check for the type returned by the function, we get that it's a Tuple even though we didn't explicitly add the parentheses on the return statement.

      Estefania.

      Delete
    2. NOTE: The blog's text editor removed the indentation, but return x, y should be indented to be considered part of the returnValues() function.

      Delete

Post a Comment