FAQ: Introduction to Functions - Multiple Return Values


This community-built FAQ covers the “Multiple Return Values” exercise from the lesson “Introduction to Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Computer Science
Data Science

FAQs on the exercise Multiple Return Values

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


On this exercise when we save the low_limit and high_limit as variables we only have to put them in as low and high? My question is do we not have to write out the entire name of low_limit and high_limit? Also can this be used to recall other variables as well where you can leave out the second word?


def get_boundaries(target, margin):
return low_limit = target - margin
return high_limit = target + margin

I don’t know why it is wrong



def get_boundaries(target, margin):
low_limit = target - margin
high_limit = target + margin
return low_limit, high_limit


I had the same question, so I changed the names from low and high to x and y, and it worked anyway. So no, it’s not about being able to shorten the name of the variable. I don’t understand how it knows what I’m trying to store in low/x and high/y, though. Is it associating low with the first returned value and high with the second?


I’m having trouble understanding the purpose of the “return” command to begin with.

In the example from the exercise:

def square_point(x_value, y_value):
  x_2 = x_value * x_value
  y_2 = y_value * y_value

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.


The discussion at this link explains the purpose of the return command in python (versus print), and I think confirms my interpretation above:


Functions that are not explicitly intended to PRINT, usually with some sort of special format, should never print, but always RETURN their value(s).

When there are two or more values they can be written in a comma separated list and will be received by the caller as a TUPLE.

>>> def foo(a, b, c):
    if a > b: a, b = b, a
    if a > c: a, c = c, a
    if b > c: b, c = c, b
    return a, b, c

>>> foo(9,8,7)
(7, 8, 9)

Note that the return value is a tuple that must be unpacked if we wish to split out the values into separate variables.

>>> a, b, c = foo(9,8,7)
>>> a
>>> b
>>> c


It’s because you only need to use return once and define the variables that you want to return beforehand.


I’m perplexed by this as well. Up until this point we had to clearly define variables, at this stage somehow the low, high are taking on new values without a logical connection (that I can tell at least).


Hi @mtf,

can you help us with: FAQ: Introduction to Functions - Multiple Return Values

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.


to be more specific, here is the code snippet:

def get_boundaries(target, margin):
  low_limit = target - margin
  high_limit = margin + target
  return(low_limit, high_limit)
low, high = get_boundaries(100, 20) 

print("Low limit: " + str(low) + ", " + "high limit: " + str(high))

How does the return know that low, high are now “low_limit” and “high_limit”?



It’s the order that the return is placed:

  return(low_limit, high_limit)

Low first and high 2nd. When you wrote :

low, high = get_boundaries(100, 20) 

it followed the same order. Low gets assigned to low_limit value and high gets assigned the high_limit value. Switch high and low like below:

high, low = get_boundaries(100, 20)

The code doesn’t know high goes to high and low goes to low just because of the name, but because of the position of the return values.

This swap would give you:

Low limit: 120, high limit: 80

Which is wrong, but shows that it’s the order the code is written that makes it line up.

Not sure if this answers your question, but hopefully it helps.


Really helpful! Thank you @monochromaticmau!


I made this exercise a little more useful, IMHO. It won’t run on codecademy BUT it will run fine on google colab

target = input("Your target? ")
margin = input("Your margin? ")
def get_boundaries(target, margin):
#always a good idea to document your functions
  """takes two integers and calculates margin of risk tolerance.
  target and margin"""
  low_limit = target - margin
  high_limit = target + margin
  return low_limit, high_limit

low, high = get_boundaries(int(target),int(margin))
print("Low limit: "+str(low)+", high limit: "+str(high)+"\n")

#let's take a look at the function definition


No idea why the system considers this wrong. The output matches the required output for the exercise.


It could be the SCT is looking for a different print statement…

print ("Low limit: {}, high limit: {}".format(low, high))

Or simply that your output does not have a colon on the second label (more likely the case).


thanks for looking into this. The output was exactly as the program had wanted it, but I was missing the closing bracket.


You could write out the whole name i.e. low_limit if you’d like but I think just to keep it simple, they used just low and high. Since we already put in calculations for the def get_boundaries. You could also write low_limit if you’d like… the calculation will still work!