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!

7 posts were split to a new topic: Difference between low, high and low_limit, high_limit?

9 posts were merged into an existing topic: What actually happens when returning multiple values in a function?

A post was split to a new topic: Suggested improvements to multiple return values exercise

3 posts were split to a new topic: My solution isn’t being accepted? (Functions with multiple return values)

2 posts were split to a new topic: Code Error (Multiple return values)

2 posts were split to a new topic: Why can’t I print variables inside the function? (Return multiple values)

I’m a very new beginner and i don’t understand return function that well and i cant do the third step can someone please help me! this is my code so far and don’t understand the third step - how do i save the returned values to high and low.

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

2 Likes

When Python is given multiple values in the return statement, it packs them. To unpack the return we assign it to corresponding variables.

     return low_limit, high_limit

 low, high = get_boundaries(target, margin)
1 Like

Hello, i have a code like this:

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, high)

print(get_boundaries(100, 20))

It outputs the answer:

(80, 120)

I have a question, why it outputs the answer with parentheses? )
Thank you!

1 Like

When you supply two values separated by a comma in Python statement you are creating a tuple. You can do a quick test of this using the following-

test_tuples = 1, 3
a, b = 1, 3
print(test_tuples)
print(a, b)
print(isinstance(test_tuples, tuple))  # proper method of checking
Output:
(1, 3)
1, 3
True

So your print statement from your function contains parantheses as this is the way tuples are displayed in the console output.

1 Like

Hi! I am a beginner as well!

You are on the right track. You just need to assign variables to the function get_boundaries (100,20) in order for you to call properly (which is what you defined). And the variables are the values you assigned to the return . Then proceed with print and don’t forget to define your results at the end. I hope I used the right terminology and I hope that makes sense!

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

low_limit, high_limit = get_boundaries(100,20)
print (low_limit)
print (high_limit)

low = 80
high = 120

2 Likes

Hi,
Is there a way to use one specific returned value from a function that returns several values, without having to save them all to variables?
Eg. if my function returned value1, value2, value3 - how can I use one of those , without having to create 3 variables separated by commas and setting them equal to the function before.

Hi @ringeflange.
You could save them to a single name and simply address the index you know contains the value you want (ideally make it clear to anyone reading your code what you are actually using from the function return)-

this_tuple = my_func()
print(this_tuple[2])  # reference you actually wanted.

It’s also not uncommon to just supply throwaway names for return values you know you don’t need, e.g.

_, _, useful = my_func()  # Here _ is used as a throwaway name.

This can be extended with tricks like unpacking, but you’d need to know exactly which values you wanted and you can start to lose readability if it’s abused-

*_, useful = my_func()  # For the last element returned.
1 Like
low, high = get_boundaries(100,20)

print(low)
print(high)

I don’t understand where how low and high are working.

As the name of the lesson suggests, get_boundaries() has multiple values in the return statement. Python packs the values in a tuple before returning them. (Actually, two or more comma separated values represent a sequence.)

When the return is assigned to comma separated variables, the sequence is unpacked and assigned in the order they were written in the return statement.

return low_limit, high_limit

is assigned to low, and high at the caller.

1 Like

why doesn’t this syntax work. instead of creating variables named low_limit and high_limit

def get_boundaries(target,margin)
return target-margin, target+margin

It may work, but it may also not be what the lesson checker is looking for. Follow the instructions for best results.

1 Like

So in what you wrote, return gives a set (x,y) nonvisible to the user, where x is a given low_limit and y is a given high_limit. Then (low, high) is set equal to (x,y). And then we individually print low and high to see x and y printed, is that accurate?

Why do we have to do the extra step of assigning the return values to other variables instead of just:

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

#like this
print(get_boundaries(100,20))

#instead of this what the lesson says
low, high = get_boundaries(100,20)
print (low, high)

As per the instructions you are aiming to save the function output to two variables, low and high. It doesn’t ask you to print any outputs to the console. Whilst you could print them out if you wanted to, should you then neglect to save those variables with the expected names you might not actually pass the testing required to complete the lesson.

In short, assign them to those names because you probably need to.

1 Like