# What does return do?

## Question

What does return do?

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 `return`s 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:

20 Likes

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.

3 Likes

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

4 Likes

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

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.

11 Likes

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 ?

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

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 `return`ed 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.)

43 Likes

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.

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

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

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?

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

Thanks. It seems I finally got it…

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

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)
``````
2 Likes

The simplest.
If you ask the function to `return` what it did, it will return.
If you do not ask, it will not return.
And honestly will write that all it agree to provide you now is just `None`. And if you want something else, ask it with `return`.
That’s all.

So I’m sure you, rwaimi, have long since learned this, but I’m chiming in because I read this whole thread, couldn’t make sense of the answers, and then still had to go on YouTube, find a good illustrative video, and watch it a couple of times very carefully before I understood what return does.

Now I think I found a way, using one of the free course’s own exercises to show, very concisely, what return is for, and I hope it helps someone:

n = “Hello”

def string_function(s):
return s + “world”

def string_function_no_return(s):
s = s + “world”

print string_function(n)
#will print what is generated by the function because the function returns the value. Calling a function that returns what it generates when given a particular argument produces the same result as simply writing the value that that you derive through the use of that function taking that argument.

print string_function_no_return(n)
#will print “None” because, although it performs the desired operation, it doesn’t connect that operation to any output. Unless you use the generated value to do something inside the function (e.g. include your print statement inside the function), the new string that the function generates is trapped inside the function, and the function returns “None”.

This is a slight misconception. The function returns nothing since it has no return statement. What is seen by the caller at the other end is this nothing, which Python translates to `None`. We see this when we print a call to a function that has no return.

``````>>> def foo(bar):
print (bar)

>>> print(foo('bar'))
bar
None
>>> foo('bar')
bar
>>>
``````

Notice when we print the return value, Python substitutes `None` so `print()` has an argument. When we do not print the return value, nothing gets sent to the console because there is nothing to send.

Oh. Thanks. That’s a little tricky.

1 Like

Yes, it takes a little wrapping around our head a couple times. We don’t really get to the meat of it until we run the code in the shell.

`None` is Python’s alias for `nothing` or `undefined`, and we see this in the shell when nothing is echoed as a final response from the function.