FunctionsBeginner7 min34 / 63

Return Values

Learn how functions send data back to you with return, why that is different from print, and how to return multiple values at once.

You have probably used a function before — maybe len() to count items in a list, or round() to tidy up a number. But have you wondered how those functions give you back a result you can actually use? The secret is the `return` statement. In this lesson you will learn how to make your own functions send data back to whoever called them — and why that is way more powerful than just printing to the screen.

See it in action

Visual walkthrough1 / 6
1

Functions Can Hand Things Back

Built-in functions like len() and round() don't just do something — they give you a result you can use. The return statement is how any function sends data back to the caller.

Think of a vending machine: you press a button and it hands back a snack you can actually keep.

#What Does return Do?

Think of it like

Think of a vending machine

You put in money and press a button. The machine does its work, then returns a snack through the slot. You now have the snack — you can eat it, share it, or save it for later. return works the same way: the function does its work, then hands something back to you that you can actually use.

When Python sees return, it immediately exits the function and sends the value back to wherever the function was called. You can store that value in a variable, pass it to another function, or use it in a calculation.

The function returns a value we store in total.
def add(a, b):
    result = a + b
    return result

total = add(3, 5)
print(total)

#return vs. print — What is the Difference?

print and return can look similar at first — both seem to "show" a value. But they are very different:

  • `print` displays text on the screen. That's it. The value disappears after it is shown.
  • `return` hands the value back to the caller. Your code can then use that value however it likes.

If you only print inside a function, the function actually gives back None — Python's way of saying "nothing".

double_print shows 8 but gives back None. double_return gives back 8 you can use.
def double_print(x):
    print(x * 2)   # just shows it

def double_return(x):
    return x * 2  # hands it back

a = double_print(4)   # prints 8, but a is None
b = double_return(4)  # b is 8, nothing printed yet

print(a)   # None
print(b)   # 8
Common mistake

Printing is not the same as returning

A very common beginner mistake: writing print(...) inside a function and then trying to use the result.

```python def square(n): print(n * n) # WRONG if you want to use the result

result = square(5) # prints 25, but result is None print(result + 1) # TypeError: unsupported operand type(s) ```

If you want to use the value later, always use return instead of print.

#Early Return — Exiting a Function Early

return does not have to appear only at the end of a function. You can use it earlier to exit the function as soon as you have the answer. This is called an early return and it can make your code cleaner and easier to read.

The function exits as soon as it knows the answer.
def is_adult(age):
    if age < 18:
        return False   # exit early
    return True        # only reached if age >= 18

print(is_adult(15))
print(is_adult(21))
Tip

Early return keeps code flat

Instead of nesting lots of if/else blocks, you can handle the special cases early and return. The "happy path" code then sits at the bottom without any extra indentation. Many experienced programmers prefer this style.

#Functions That Return Nothing

If a function has no return statement — or just a bare return with no value — Python automatically gives back None. This is perfectly fine for functions whose job is to do something (like saving a file or printing a greeting) rather than compute something.

Both functions give back None because they do not return a value.
def greet(name):
    print(f"Hello, {name}!")
    # no return statement

def nothing():
    return   # bare return, same result

x = greet("Alice")
print(x)

y = nothing()
print(y)

#Returning Multiple Values

Sometimes a function needs to hand back more than one piece of data. Python makes this easy — you can return multiple values separated by commas. Under the hood Python packs them into a tuple, but you can unpack them instantly into separate variables.

Two values come back, unpacked into two variables in one line.
def min_max(numbers):
    return min(numbers), max(numbers)

lowest, highest = min_max([4, 1, 9, 2, 7])
print(lowest)   # 1
print(highest)  # 9
Returning two strings and unpacking them on the left side of =.
def split_name(full_name):
    parts = full_name.split(" ", 1)
    first = parts[0]
    last = parts[1] if len(parts) > 1 else ""
    return first, last

f, l = split_name("Ada Lovelace")
print(f"First: {f}, Last: {l}")
Note

The returned tuple is real

When you write return a, b, Python actually returns a single tuple (a, b). You can keep it as a tuple if you like:

``python result = min_max([4, 1, 9]) print(result) # (1, 9) print(result[0]) # 1 ``

Unpacking into separate variables (a, b = func()) is just a convenient shortcut.

#Putting It All Together

Early return handles the error case; the normal result comes at the end.
def safe_divide(a, b):
    if b == 0:
        return None   # early return for bad input
    return a / b

result = safe_divide(10, 2)
if result is not None:
    print(f"Answer: {result}")

bad = safe_divide(5, 0)
print(bad)
Quick check

What does a function return if it has no return statement?

Key takeaways

  • `return` sends a value back to the caller so your code can use it — `print` only displays it on screen.
  • A function with no `return` statement (or a bare `return`) gives back `None`.
  • You can return early to exit a function the moment you have the answer, keeping your code clean.
  • Returning multiple values with commas creates a tuple you can unpack into separate variables instantly.
Practice challenges
Test yourself · earn XP
0/4
Predict the output#1

What does this code print?

predict-output
def double_print(x):
    print(x * 2)

def double_return(x):
    return x * 2

a = double_print(3)
b = double_return(3)
print(a)
print(b)
Fix the bug#2

This code has a bug. The programmer wants result to hold the squared value so they can use it in a calculation — but it doesn't work. What is wrong?

fix-bug
def square(n):
    print(n * n)

result = square(5)
print(result + 1)
Fill in the blank#3

Complete the function so it returns both the sum and the product of two numbers, then unpacks them into two variables.

def sum_and_product(a, b):
     a + b, a * b

total, product = sum_and_product(3, 4)
print(total)
print(product)
Reorder the lines#4

Put these lines in the right order so the function returns False early when age is under 18, and True otherwise.

1
print(is_adult(20))
2
    return True
3
def is_adult(age):
4
        return False
5
    if age < 18:
Your turn
Practice exercise

Write a function called circle_stats that takes a radius (a number) and returns TWO values: the circumference and the area of the circle. Use the formulas: circumference = 2 3.14159 radius and area = 3.14159 radius radius. Then call the function with a radius of 5 and print both results.

Try it live — edit the code and hit Run to execute real Python:

solution.py · editable