Returning Values
So far we've mainly been discussing functions which perform some internal process, print the result, and then end. However, many of the functions we use regularly in Python don't print anything, so how do we get values out of them?
len
is a good example. We provide len
some argument, and then len
spits out a value. We can even assign len
to a variable so that we can use the value later on.
The answer is that we use a return
statement.
What does a return statement do?
A return
statement actually does two things.
- It ends the execution of the function.
- It returns some value.
By default, the return
statement returns the value None
, but we can override this default to return some other information.
Even if we don't use a return
statement, all functions return something.
Remember that the default return value is None
, so the add
function we defined earlier in this chapter actually returns None
, despite not using an explicit return
statement.
Using a return statement
Let's modify our add
function to use an explicit return
statement.
Adding a return
statement is as simple as writing return
at the end of the function body.
def add(x, y):
result = x + y
print(result)
return # returns None
This isn't a very useful return
statement, however. Our function already ended its execution at this point, as there was no more code to run, and we haven't provided a return value, so add
still just returns None
.
Let's replace the print
function inside add with a more useful return
statement. Return values are simply placed after the return
statement.
def add(x, y):
result = x + y
return result
So what happens when we call add
now? Well, the function runs as normal, and then add
returns the value of result
. However, we're not printing anything anymore.
This sort of structure allows us to do several other things though. We can assign the result of calling add
to a variable:
add_result = add(2, 3) # 5
We can also pass the result of calling add
into print
as an argument.
print(add(1, 6)) # 7
Cool stuff! You can try it out on this editor below:
Using multiple return statements
Earlier I said that adding a return
statement was as simple as writing return
at the end of the function body, but you can actually put return
anywhere in your function.
One thing to keep in mind is that if you put a return
statement before some other code in your function body, the function will end before that other code ever runs.
For example, in the following example, we never get to assign x + y
to the variable result
.
def add(x, y):
return
result = x + y
This kind of defeats the point of the add
function. However, there are cases when we might want to exit a function early based on some condition.
A few chapters back, we discussed if statements and using conditionals. You can place return
statements inside a conditional block when they're used inside a function.
For example, we might want to do something like this:
def divide(x, y):
if y == 0:
return "You tried to divide by zero!"
else:
return x / y
When a user tries to divide by zero, instead of attempting the division, we just return a message, informing them of their mistake.
print(divide(10, 2)) # 5
print(divide(6, 0)) # You tried to divide by zero!