What does return do?


#1

Question

What does return do?

Answer

It sounds silly, but return does literally what it says – it returns something to wherever the function was called in the program. If we consider the square() function in this exercise, for example, it does some calculation with the number given to the function as n, and then returns the final result called squared.
This allows us to do lots of useful things! When we return a value it can be stored in a variable for later use, or used on the spot, or any number of things. It basically makes your function call a usable value!
Take a look at the code below for an example:

def square(n):
  """Returns the square of a number."""
  squared = n ** 2
  return squared


my_number_squared = square(10)
print my_number_squared  # Prints 100 
print square(10)  # Also prints 100!

We not only used the returned value to store a value in a variable, but we also used it with a built in method that takes a value and prints it, print! Pretty handy!


If you’re still confused, @benenicktus wrote an alternate explanation below:


FAQ: Learn Python: Functions - Call and Response
FAQ: Learn Python: Functions - Review: Built-In Functions
#3

Hi,
I actually still dont understand why do we need Return really !
if you get rid of it you will see that nothing changes, the output still the same! when I call the fuction defined wheather or not I inculde the Return the output still the same.
Can someone explain please !


#4

Agree that the example above outputs 100 twice?

now lets remove return:

def square(n):
  """Returns the square of a number."""
  squared = n ** 2

my_number_squared = square(10)
print my_number_squared  # Prints None
print square(10)  # Also prints None

run the code or see the comments for output. so return does literally what is say, its returns/hands back the values. This is very useful, given squared is a local variable within the function, without return we wouldn’t be able to access it outside the function


#5

True, however, when I try when the example below, taking out the Returndoesnt change anything !
image


#6

because you do nothing with the returned value.

However, its more complicated that that. return is a control flow construct, to pass data around. And not specific for this example, but as programs grow larger, control flow is such important. You need separate functions for maintainability. (you do seriously not want a 10,000 line single file with no function or classes, its a nightmare).

so understanding return is very important, you are going to need it later, a lot.

That is always the problems with these concepts, its not just specific about this exercise, its about the bigger picture. Something which is difficult to explain.


#7

If i use print square(10) at the bottom and does not use return it doesn’t give correct result.

Is it because function square is not getting any value in return ?


#8

yea, then the default (None) is returned. Functions always return a value


#9

Hi,
I’m all new to programming and didn’t understand the explanation/answer at the top either.

But I do think I understand it now, after reading trough the comments.

A function will always return a value. As a programmer you have to define which value will be returned by the function. If you don’t define this, the function will automatically return ‘None’

def more_complicated(n):
  """Returns the square of a number and adds 2 to that number."""
  squared = n ** 2
  complication = squared + 2


print more_complicated(10)
  

In the above example the function will return ‘None’ , because it doesn’t know which step of the calculation to return. I didn’t tell him!
Does it have to return squared or does it have to return the calculation after that? The programmer decides, not the function.

def more_complicated(n):
  """Returns the square of a number and adds 2 to that number."""
  squared = n ** 2
  complication = squared + 2
  return complication

print more_complicated(10)
  

Adding return complication will make the function return 102. And for whatever reason I, as a programmer, could have decided to return squared and the function would have returned 100.

def more_complicated(n):
  """Returns the square of a number and adds 2 to that number."""
  squared = n ** 2
  complication = squared + 2
  print complication 


more_complicated(10)
  

This piece of code will print 102. How is that possible? Because the code doesn’t actaully call the answer of the function. The print statement is inside of the function.

Adding print more_complicated(10) further along in your code (outside of the function), will return ‘None’ because inside of the function, nothing was returned. (and it will also print 102 again, because the print statement is still inside of the function.)


#10

Hello there,

I have a question. Here is my example of fuction definition:

def square(n):
“”“Returns the square of a number.”""

squared = n ** 2
return squared

Question: why do we

print (square(10))

but not

print (squared)

?
square is variable created inside a function square(). Because of this, I cannot understand how to use return without looking for specific examples that fits my code.


#11

squared has a local scope, it only exists within the function.

furthermore, for the function to execute you need to call the function.


#12

Hmm… I will ask a little different. Let’s say my code looks like this.

squared = 1
def square(n):

    squared = n ** 2
    return squared

print (square(10))
print (squared)

As I know squared value would not change after calling function and code would print:

100
1

How could I change the value of squared using function?


#13

you could update the squared variable by doing:

squared = square(10)

this will store the returned value in a variable, not print the returned value


#14

Thanks. It seems I finally got it… :slight_smile:


#15

Thank you for your explanation. They should take your explanation to replace theirs.


#16

I was having such trouble at first, even after reading the posts, then i realized, return just passes whatever you want to pass back to the call. It doesn’t matter what is happening inside the function, it can be calculating world peace but it won’t matter unless you tell it to return that value.

n is just what is being passed to the function when you call it. You don’t even have to do something with it.

In this instance, n = 6 and is passed to the function but i never used it. What is returned has nothing to do with the function itself at all. The point is, this is very flexible, you don’t need to feel trapped into the exercise.

def square(n):
  """not (Returns the square of a number.) """
  squared = 10 ** 2
  squared_plus = squared + 2
  print  squared, squared_plus
  return "hi, i have nothing to do with this function"
  

print square(6)