return

Updated March 16, 2026 · Keywords
keyword function control-flow

The return keyword is used inside functions to exit the function and optionally pass a value back to the code that called the function.

Syntax

def function_name():
    # function body
    return value  # optional value to return

How It Works

When Python encounters a return statement, it immediately exits the function and sends the specified value back to the caller. If no value is specified, the function returns None.

def greet():
    return "Hello!"

result = greet()
print(result)
# Hello!

def no_return():
    x = 1 + 1

result = no_return()
print(result)
# None

Returning Values

You can return any Python object:

# Return a string
def get_message():
    return "Welcome back!"

# Return a number
def calculate_total(prices):
    return sum(prices)

# Return a list
def get_numbers():
    return [1, 2, 3, 4, 5]

# Return a dictionary
def get_user():
    return {"name": "Alice", "age": 30}

# Return a tuple (useful for multiple values)
def get_stats(numbers):
    return min(numbers), max(numbers), sum(numbers)

min_val, max_val, total = get_stats([1, 2, 3, 4, 5])
print(min_val, max_val, total)
# 1 5 15

Early Exit

return lets you exit a function before reaching the end:

def find_first_even(numbers):
    for num in numbers:
        if num % 2 == 0:
            return num  # Exit immediately when found
    return None  # No even number found

print(find_first_even([1, 3, 5, 6, 7]))
# 6

print(find_first_even([1, 3, 5, 7]))
# None

Guard Clauses

A common pattern is using return early to handle edge cases:

def process_user(user):
    # Guard clause: validate input
    if not user:
        return None
    
    if not user.get("name"):
        return None
    
    # Main logic here
    return f"Processing {user['name']}"

Return vs Print

Beginners often confuse return with print. Here’s the difference:

def add(a, b):
    print(f"Adding {a} + {b}")  # prints but doesn't return
    a + b  # result is discarded!

result = add(1, 2)
print(result)
# Adding 1 + 2
# None

def add_correct(a, b):
    return a + b

result = add_correct(1, 2)
print(result)
# 3

Returning Multiple Values

Python functions can return multiple values as a tuple:

def divide(a, b):
    if b == 0:
        return None, "Cannot divide by zero"
    
    quotient = a // b
    remainder = a % b
    return quotient, remainder

result = divide(10, 3)
print(result)
# (3, 1)

quotient, remainder = divide(10, 3)
print(f"Quotient: {quotient}, Remainder: {remainder}")
# Quotient: 3, Remainder: 1

Returning Functions (Closures)

You can return a function from a function:

def multiplier(factor):
    def multiply(x):
        return x * factor
    return multiply

double = multiplier(2)
print(double(5))
# 10

triple = multiplier(3)
print(triple(5))
# 15

Return and Lambda

Lambda functions implicitly return the result of their expression:

# These are equivalent
add_lambda = lambda a, b: a + b

def add_function(a, b):
    return a + b

See Also