return keyword is used in functions. The
return keyword exits a function and returns a specified value.
return allows local values to be carried out of the scope of a function and be used in the rest of the program.
Functions are blocks of instructions that perform a specific task in programming. Once a function has been declared, it can be reused multiple times. The main benefit of using functions is that it can reduce the number of lines of code you write because you can reuse common functions multiple times over.
When you’re working with functions in Python, you will encounter the
return statement, which is used to return values from a function.
In this tutorial, we are going to discuss how the
return statement works, and how it can be used to return values within a Python function. We will also explore a few examples of the
return statement being used in a function.
Python Function Refresher
Python includes a number of built-in functions—such as
len()—which perform specific tasks. In addition, you can also declare your own functions which can be used to proclaim a specific block of code that can be reused throughout your program.
In Python, user-defined functions are declared using the
def keyword. After using the
def keyword, you should state the name of your function, followed by a set of parentheses which can hold the parameters that your function will hold.
Here’s the syntax for declaring a function in Python:
Let’s explore an example of a basic function in Python. Let’s say that we are the owner of a bakery and we want to write a function that prints
Welcome, we’re open! to the console. We want this code to be in a function because we intend to use it multiple times. Here’s an example of a function that would accomplish this task:
def printWelcomeMessage(): print("Welcome, we're open!")
If we execute our program, nothing will happen. This is because we have not yet called our function. In order to see our welcome message, we need to call our function using
printWelcomeMessage() like this:
Our code returns:
Welcome, we're open!
In this example, we have printed out a string to the console. But what if we want to return a value that can be used in our program? That’s where the
return keyword comes in.
return keyword can be used to exit a function and return a particular value.
This is useful because it allows us to process data within a function, then return it so that we can use that data throughout our program. Data processed within a function is only accessible within the function—it has what is called
local scope—unless you use the
return statement to send the data elsewhere in the program.
Here’s the syntax for the return keyword:
def example_function(): return "Example"
return keyword can appear anywhere within a function, and when the code executes the return statement, the function will be exited.
The return statement is usually used to return a value to the rest of the program, but it can also be used alone. If no expression is defined after the return statement,
None will automatically be returned. Here’s an example of this in action:
def example_function(): return example_function()
Our code returns:
Python Return Examples
Let’s go through two examples to show how the
return statement can be used.
Say that we are operating a restaurant and we want to create a program that returns the cost of the sandwich meals we have on the menu. This task should be contained within a function because we intend to use it more than once. The waitress should enter the name of the sandwich a customer has ordered, and be presented with the price of the sandwich.
We could accomplish this task using the following code:
sandwich = "Ham and Cheese" def calculate_price(product): if product == "Ham and Cheese": return 2.50 elif product == "Tuna and Cucumber": return 2.25 elif product == "Egg Mayo": return 2.25 elif product == "Chicken Club": return 2.75 else: return 2.50 calculate_price(sandwich)
Our code returns:
There’s a lot going on in our code, so let’s break it down. On the first line, we declare the name of the sandwich whose price we want to find out. In this case, our
sandwich variable stores the value
Ham and Cheese.
Next, we declare a function called
calculate_price() which accepts one parameter: the name of the sandwich the customer would like to purchase. In this function, we have used
if statements to check the name of the product against our list of sandwiches. When the product name is found, our program returns the price it has defined.
So, if our sandwich is “
Ham and Cheese”, our program returns 2.50; if our sandwich is “
Chicken Club”, our program returns 2.75; if our sandwich is not listed, the “
else” returns 2.50.
On the final line, we call our
calculate_price() method and pass the sandwich variable as an argument. When our
calculate_price() method is called, our program executes the method and returns the price for the sandwich we have specified.
In this case, the sandwich our customer has ordered is
Ham and Cheese, so our program returns 2.50.
As soon as the
return statement is executed, our function stops and our program continues to run.
Let’s use another example of the
return statement to illustrate how it works. Say that we are creating a program that calculates the change a customer is due. We could accomplish this task using the following code:
price = 2.50 def calculate_change(amount_given): return amount_given - price calculate_change(4.00)
Our code returns:
In our code, we declared a function called
calculate_change that subtracts the price of the sandwich from the amount given by the customer. In this case, the price of the sandwich is $2.50, and the user has given the waitress $4.00. So, our program returns $1.50.
The return keyword in Python can be used to exit a function and return a value. This allows you to send a value from a function (which has local scope) to the main program so that it can be accessed everywhere in your code.
In this tutorial, we delved into the basics of the Python return statement. We also walked through two examples of Python functions that utilised the return keyword to illustrate how the statement works. Now you’re prepared with the knowledge you need to use the return keyword like a master!