Blog Post #91: Getting Data Back: The return Statement

Up to this point, our custom functions have performed actions—they’ve printed banners or messages to the console. They do things, but they don’t give us anything back that we can store and use later in our program.

What if we want a function to perform a calculation and hand us the result? How do we get a value out of a function?

The answer is the return statement. In this post, you’ll learn how to use the return keyword to send a value from your function back to the code that called it.

What is a return Statement?

The return statement is a keyword that you use inside a function. It has two primary jobs:

  1. It immediately exits the function. The moment Python executes a return statement, the function stops running.
  2. It sends a specified value or object back to the caller—the line of code that originally called the function.

Any function that has a return statement is sometimes called a “fruitful” function because it “bears fruit”—it produces a result that can be used elsewhere.

A Simple return in Action

Let’s write a function that is designed not to print anything, but to perform a calculation and give us the answer.

def add_numbers(a, b):
    """This function takes two numbers and returns their sum."""
    result = a + b
    return result

Now, let’s call this function and, most importantly, capture its return value in a variable.

# Call the function with arguments 5 and 3
sum_value = add_numbers(5, 3)

print(f"The function returned: {sum_value}")

The output will be: The function returned: 8.

Here’s the flow of data:

  1. The arguments 5 and 3 are passed into the add_numbers function.
  2. Inside the function, result is calculated as 8.
  3. The return result statement sends the value 8 back out of the function.
  4. The assignment statement sum_value = ... “catches” that 8 and stores it in the sum_value variable.

return Immediately Exits the Function

An important property of return is that it causes the function to stop executing immediately. Any code inside the function that comes after a return statement is considered “unreachable” and will never run.

def add_numbers_with_unreachable_code(a, b):
    result = a + b
    return result
    # This line will NEVER be executed
    print("This message is after the return statement.")

output = add_numbers_with_unreachable_code(10, 20)
print(f"The output was: {output}")

The “unreachable” message will never appear on your screen. This is a useful feature for controlling the flow within your functions.

Using Return Values Directly

You don’t always have to store a returned value in a variable. You can use the result of a function call directly in any expression where a value is expected.

# Using the returned value directly inside an f-string
print(f"The sum of 10 and 20 is {add_numbers(10, 20)}.")

# Using the returned value as part of another calculation
new_result = add_numbers(5, 3) * 2
print(f"The new result is: {new_result}") # Output will be 16

What’s Next?

The return statement is the fundamental way for functions to produce output that can be used by the rest of your program. It allows you to create self-contained blocks of logic that calculate a value and hand it back, making your code much more modular and powerful.

We’ve now seen what happens when a function has an explicit return statement. But what about the functions we wrote earlier that only had print() statements and no return? Do they return anything at all? In Post #92, we will investigate what happens when you don’t explicitly return a value and meet Python’s special None type.

Author

Debjeet Bhowmik

Experienced Cloud & DevOps Engineer with hands-on experience in AWS, GCP, Terraform, Ansible, ELK, Docker, Git, GitLab, Python, PowerShell, Shell, and theoretical knowledge on Azure, Kubernetes & Jenkins. In my free time, I write blogs on ckdbtech.com

Leave a Comment