Return
Resources
Python functions are also capable of returning a value, In Python, we use the return
keyword.
Let’s look at a quick example of a full Python program that includes a function that returns a value:
def square_sum(one, two):
one = one * one
two = two * two
total = one + two
return total
def main():
text_one = input("Enter the first number: ")
one = int(text_one)
text_two = input("Enter the second number: ")
two = int(text_two)
total = square_sum(one, two)
print(f"The sum of squares of {one} and {two} is {total}")
main()
To truly understand how this program works, let’s use Python Tutor to explore it step by step. Like before, copy the code into Python Tutor, or click this Python Tutor link.
At the start, our Python Tutor trace will look like this:
The first few steps are pretty straightforward, since Python will simply move through the code and record all of the functions it finds. Once we reach the call to the main()
function at the bottom, we’ll be at this state:
So, we’ll enter the main()
function and start executing the code it contains. The first line is an input()
expression, so Python will prompt the user for input. In Python Tutor, this will open a box at the bottom where we can enter the input, as shown below:
Let’s assume that the user would enter 2
for this input. So, we can type that into the box and press ENTER or click submit. Python tutor will refresh the page to accept the input, and then we’ll be at this setup:
Next, we’ll convert the string value "2"
that is currently stored in the text_one
variable to an integer using the int()
function and store it in the variable one
. On the next line, it will ask for input once again:
In this case, we’ll assume the user is inputting 3
, and then we’ll store it and convert it to an integer stored in the variable two
:
At this point, we’re ready to call the square_sum()
function. So, Python Tutor will find the arguments for the function call and prepare to store them in the parameter variables of the function. When we click next, we’ll see this setup in our window:
Recall that Python Tutor will create a new frame for the square_sum()
function in the frames area and store the variables from that function there. This will become important later when we reach the end of the function. Inside of the square_sum()
function, we just perform a few mathematical operations, culminating with the total
variable storing the sum of the squares of the two arguments, as shown here:
At this point, we’ve reached the return
statement at the end of the square_sum()
function. When we click next on Python Tutor, we’ll see something new appear:
When Python reaches the end of a function, it will record the return value in the function’s frame before leaving the function. This value is what is given back to the main()
function. So, when we click next again, we’ll see this state:
We’re back in the main()
function, and now we can see that the new total
variable stores the value $13$, which was the returned value from the square_sum()
function. So, on this last line, we’ll see that the program will create an output statement using a template string and the string format()
method, and it will display output to the user:
What’s also interesting to note here is the return value of the main()
function. Since we didn’t include a return
statement at the end of the function, the return value is set to a special value called None
in Python. We’ll learn about what that value actually represents in a future course.
The entire process is shown in the animation below:
This review of how a function returns data should be very helpful as we continue to build more complex programs. Remember - we can always paste any Python code into a tool like Python Tutor to run through it step-by-step and see what it really does.