Python Return Statement

In Python, the return statement is used to send a result from a function back to the place where it was called. This allows you to use the result of a function in other parts of your code, making your programs more powerful and flexible. It’s one of the key features that helps organize code and makes it reusable.

Here are some common ways to use the return statement:

  • Returning values: Pass a value from a function back to the caller so it can be used elsewhere.
  • Improving code reusability: Functions with return values can be reused in different parts of your program.
  • Working with calculations: Use return to send back results of mathematical operations or any function that performs a task.


What You'll Learn

You will learn the basics of the return statement in Python, including how to return values from functions and how this can be useful for simplifying your code. We’ll cover basic examples first and move on to more complex usage in later lessons.


Understanding the return Statement

The basic syntax for the return statement is:

python
def function_name():
    # code
    return expression
  • expression: This is the value or result that you want to return from the function. It could be a variable, a calculation, or a literal value like a number or string.

The return statement ends the function and sends the specified result back to the caller. Once the function returns a value, it can be used anywhere that the function was called, like assigning it to a variable or directly printing it out.


Example 1: Returning a Simple Value

In this example, we’ll create a function that simply returns a message when called:

python
def greet():
    return "Hello, World!"
              
print(greet())

How It Works:

  • greet: This is a simple function that returns the string "Hello, World!".
  • return: The return statement sends the string back to where the function was called.
  • print(greet()): The result of calling greet() is printed to the screen, which shows "Hello, World!".

Output

Hello, World!

Example 2: Returning a Calculation Result

Functions can also return the results of calculations. Here's an example of a function that returns the sum of two numbers:

python
def add_numbers(a, b):
    return a + b

result = add_numbers(3, 5)
print(result)

How It Works:

  • add_numbers: This function takes two parameters, a and b, and returns their sum.
  • return a + b: The return statement returns the result of adding a and b.
  • result = add_numbers(3, 5): The returned value is stored in the variable result.
  • print(result): The value stored in result (which is 8) is printed to the screen.

Output

8

Example 3: Returning Multiple Values

In Python, a function can return multiple values as a tuple. Here’s an example:

python
def get_coordinates():
    return 10, 20

x, y = get_coordinates()
print(x, y)

How It Works:

  • get_coordinates: This function returns two values, 10 and 20, as a tuple.
  • return 10, 20: The function sends back both values, which can be assigned to separate variables.
  • x, y = get_coordinates(): The two returned values are unpacked into x and y.
  • print(x, y): The values of x and y (10 and 20) are printed to the screen.

Output

10 20

Exercises

Try out the following exercises to practice using the return statement.

1. Write a function that returns your name.
python
# Exercise 1: Return your name
def get_name():
    return "Your Name"

print(get_name())

2. Write a function that returns the square of a number.
python
# Exercise 2: Return the square of a number
def square_number(number):
    return number ** 2

print(square_number(4))

3. Write a function that returns the maximum of two numbers.
python
# Exercise 3: Return the maximum of two numbers
def max_of_two(a, b):
    return a if a > b else b

print(max_of_two(10, 20))

*Tip: After completing an exercise, experiment with returning different types of data, like strings, lists, or even other functions!


Frequently Asked Questions

What is the return statement in Python?

The return statement in Python is used to send a result from a function back to the place where it was called. It allows the function's result to be used elsewhere in the program.


Can a Python function return multiple values?

Yes, a Python function can return multiple values as a tuple. These values can be unpacked when calling the function to assign them to separate variables.


What happens if a function doesn’t have a return statement?

If a function doesn’t have a return statement, it will return `None` by default. This means the function will not send any meaningful result back.


What is the difference between returning a value and printing it in Python?

Returning a value makes it available for use in the program, while printing it simply outputs the value to the console without returning anything for further use.


Can I return a calculation from a function?

Yes, you can perform a calculation inside a function and directly return the result of the calculation using the return statement.



What's Next?

Next, you'll learn about the pass statement in Python, which acts as a placeholder in your code. You'll see how it's used in situations where a statement is syntactically required but no action is needed—such as in empty functions, loops, or conditionals during development.