← Back to Python Course | Chapter 4: Functions | Lesson 3 of 9

Python Return Values

The return Statement

return sends a value back to whatever code called the function, and execution of the function stops the instant return runs — any code written after it in the same block never executes.

Example: The return Statement

python
def add(a, b):
    return a + b
    print("This never runs")

print(add(2, 3))

Implicit return None

A function with no return statement at all implicitly gives back None once it finishes; writing a bare return with no value does exactly the same thing, explicitly ending the function early while still returning nothing meaningful.

Example: Implicit return None

python
def log(message):
    print(message)

result = log("hi")
print(result)

Returning Multiple Values

Writing return a, b looks like it's returning two separate values, but Python actually packs them into a single tuple automatically — the caller can then unpack that tuple back into two variables with x, y = my_function().

Example: Returning Multiple Values

python
def min_max(numbers):
    return min(numbers), max(numbers)

x, y = min_max([3, 1, 4, 1, 5])
print(x, y)

Returning Complex Data Types

A function can return any Python object at all — a list, a dictionary, a custom class instance — not just simple numbers or strings, which is what makes functions composable building blocks for larger programs.

Example: Returning Complex Data Types

python
def make_user():
    return {"name": "Alex", "age": 30}

user = make_user()
print(user)

Early return

Returning as soon as an invalid input or edge case is detected — rather than nesting the rest of the function inside an else — is a common style called a "guard clause," and it usually keeps the main logic flatter and easier to follow.

Example: Early return

python
def safe_divide(a, b):
    if b == 0:
        return None
    return a / b

print(safe_divide(10, 0))

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.