Python Return Values

In the last lesson, we learned about Python Function Arguments. Now let’s send a result back out of a function so we can store and reuse it.

Let’s Talk About Giving Back

In the last few articles, we learned how to define functions and pass arguments to them. Our functions mostly did one thing: they printed something. But printing is not always what we want.

Sometimes we want a function to do some work and give us back a result. That result might be used later in our program. Or we might want to store it in a variable. Or pass it to another function.

That is what return values are for.

What Is a Return Value in Python?

A return value is the result that a function gives back to whoever called it.

When we call a function, we can capture that result and use it.

Think of it like a vending machine. We put in money. We press a button. The machine gives us a snack. That snack is the return value.

We do not always eat it right there. We can take it with us. Save it for later. Give it to someone else. That is what we can do with a return value.

How to Return a Value in Python

In Python, we use the return keyword to send a value back from a function.

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

This function does not print anything. It just calculates a + b and returns the result.

When we call it, we can capture the result in a variable.

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

Output

8

So result gets the value 8. Then we print it.

We can also use the return value directly without storing it.

print(add(10, 20))

Output

30

Or pass it to another function.

print(add(add(1, 2), 3))

Output

6

See how flexible it is? The return value can be used anywhere a value can be used.

return vs print in Python

This is a common confusion. Let me make it very clear.

  • print shows something on the screen. It does not give anything back.
  • return gives a value back to the caller. It does not show anything on the screen.

Let’s compare.

def add_print(a, b):
print(a + b)
def add_return(a, b):
return a + b

Now if we call add_print(5, 3), it prints 8. But if we try to store it, we get nothing.

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

Output

8
None

Wait. Why None? Because add_print does not return anything. So result gets None. But the print inside the function still shows 8.

Now with add_return:

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

Output

8

No print inside the function. But the value is returned. And then we print it ourselves.

So the key difference:

  • print is for showing something to the user.
  • return is for giving a value back to the program.

Most of the time, we want return. Because we want to use the result somewhere else. Printing is just one way to use it.

A Function Can Return Any Type in Python

We can return numbers, strings, booleans, lists, tuples, dictionaries, or even other functions. Any Python object can be returned.

def get_name():
return "Alex"
def is_even(n):
return n % 2 == 0
def get_numbers():
return [1, 2, 3, 4, 5]

That is fine.

Returning Multiple Values in Python

Python lets us return multiple values at once. We just separate them with commas.

def get_min_max(numbers):
return min(numbers), max(numbers)

This returns two values. When we call it, we can unpack them into two variables.

minimum, maximum = get_min_max([4, 2, 9, 1, 7])
print(f"Min: {minimum}, Max: {maximum}")

Output

Min: 1, Max: 9

What is actually happening? Python is returning a tuple. And we are unpacking it into two variables.

We can also capture the tuple as a single variable.

result = get_min_max([4, 2, 9, 1, 7])
print(result)

Output

(1, 9)

So it is a tuple. But unpacking is very convenient.

A Function Without return in Python

If a function does not have a return statement, it returns None.

def say_hello():
print("Hello")
result = say_hello()
print(result)

Output

Hello
None

So say_hello prints “Hello” and returns None. That is the default.

Early Return in Python

We can have multiple return statements in a function. As soon as one runs, the function stops and returns that value.

def check_number(n):
if n > 0:
return "Positive"
elif n < 0:
return "Negative"
else:
return "Zero"

This is very common. We check conditions and return early. That makes the code cleaner. No need for extra else blocks.

Python Return Values: A Real-Life Example

Let’s build a function that calculates the area of a circle and returns it.

import math
def circle_area(radius):
return math.pi * radius ** 2
area = circle_area(5)
print(f"Area: {area:.2f}")

Output

Area: 78.54

Then we can use that area for other calculations, like the volume of a cylinder.

height = 10
volume = area * height
print(f"Volume: {volume:.2f}")

That is the power of return values. They let us chain calculations together.

Common Mistakes with Python Return Values

Mistake 1: Forgetting to return

def add(a, b):
a + b # No return
result = add(5, 3)
print(result) # None

So what do we do? We add the return.

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

Mistake 2: Using print instead of return

def add(a, b):
print(a + b)
result = add(5, 3)
print(result) # None

So what do we do? We use return instead.

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

Mistake 3: Returning Too Early

def add(a, b):
return a
return b # This never runs

The second return is unreachable. Only the first one matters.

So what do we do? We make sure only one return runs.

Mistake 4: Using return Outside a Function

return 5 # Error!

return must be inside a function.

Next, we will learn about Python Default Parameters and make selected function arguments optional.