Python Return Values

In the last lesson you learned about Function Arguments. That is how you send information into a function. Now we look at the other direction. How does a function send something back out to you? That is the job of return. And once you have an answer back in your hands, you can store it. You can print it. You can feed it into more math.

πŸ€” Why You Need return

Say you write a function that adds up a shopping cart. It does the math and prints the total. Looks fine, right? Then a week later you want to add tax to that total. Now you are stuck. The number was printed on the screen and then it was gone. You never got to keep it.

That is the pain. A function that only prints gives you something to look at. It does not give you something to use.

The fix is one word. It is return. With return, the function hands the value back to whoever called it. So you can catch that value. You can put it in a variable. And you can keep working with it.

πŸ†š print and return Are Not the Same

This is the part that trips up almost everybody in the beginning. So let us slow down here.

  • print() puts text on the screen for a human to read. That is all it does. It does not give your code anything back.
  • return sends a value back to the line that called the function. Your code receives it and can do more with it.

One is for your eyes. The other is for your program.

Here is a function that only prints. We call it and try to save the result.

def add(a, b):
print(a + b)
result = add(3, 4)
print("result is:", result)

It does print the sum. But look at what result holds.

Output

7
result is: None

The 7 showed up because of the print() inside the function. But result is None. The function never handed a value back. So there was nothing to store. We will explain that None in a moment.

Now the same idea with return.

def add(a, b):
return a + b
result = add(3, 4)
print("result is:", result)

This time the value actually comes back to us.

Output

result is: 7

See the difference? With return, result is a real number now. We can add to it. We can compare it. We can send it into another function. It is ours to use.

Note

A handy way to remember it. print talks to the person. return talks to the program.

πŸ“¦ How the Caller Catches the Value

When a function hits a return, it does two things. It stops running right there. And it sends the value back to the spot where the function was called. That spot is called the caller. That just means the line of code that ran the function.

So take this line.

result = add(3, 4)

It works in a simple order:

  • Python runs add(3, 4) first.
  • Inside, return a + b sends back 7.
  • That 7 takes the place of add(3, 4).
  • So the line becomes result = 7. Now result holds 7.

Because the returned value just becomes a number sitting there, you can use it anywhere a number is allowed. You can do more math right away.

def add(a, b):
return a + b
total = add(10, 5) + add(2, 2)
print(total)

Each add(...) turns into its own number. Then those two numbers are added together.

Output

19

You could never do that with a function that only prints. You cannot add two printed messages together.

πŸ›’ A Real Example: Returning a Total Price

Let us build something closer to real life. Imagine a small online store like Amazon. A customer buys some quantity of an item at a certain price. We want a function that gives back the final total. Then the rest of the program can use it.

We will return the total instead of printing it.

def total_price(quantity, unit_price):
total = quantity * unit_price
return total
cart_total = total_price(3, 250)
print("Items total:", cart_total)

Walk through it line by line.

  • total_price takes how many items and the price of one item.
  • Inside, it multiplies them and stores the result in total.
  • return total hands that number back to the caller.
  • cart_total catches it. So now the total lives in a variable we own.

Output

Items total: 750

Now comes the part that makes it worth doing. Because we kept the total, we can keep working with it. Let us add a delivery fee and some tax on top.

def total_price(quantity, unit_price):
return quantity * unit_price
cart_total = total_price(3, 250)
delivery = 40
tax = cart_total * 0.05
final_amount = cart_total + delivery + tax
print("Final amount to pay:", final_amount)

The function gave us a clean number. Then the rest of the code did its own thing with that number.

Output

Final amount to pay: 827.5

This is the whole reason return matters. The function does one clear job and gives back a result. Everything else is free to build on top of it. If this had only printed 750, we could never have added the tax.

πŸ•³οΈ No return Gives Back None

Here is a question that confuses a lot of people. What does a function give back if you never write return?

The answer is a special value called None. None is Python’s way of saying β€œnothing here, no real value.” Every function returns something. And if you do not say what, Python quietly returns None for you.

Here is a function that only greets and never returns anything.

def greet(name):
print("Hello", name)
answer = greet("Alex")
print("answer holds:", answer)

Watch what answer ends up being.

Output

Hello Alex
answer holds: None

The greeting printed, sure. But greet had no return. So answer got None. That is exactly why our very first add example earlier also gave back None. It printed, but it never returned.

Caution

If you store a function’s result and get None, check the function. Most of the time it means you used print inside it where you meant to use return.

A bare return with nothing after it also gives back None. People use it to leave a function early. We will see more of that later.

πŸ“‹ print vs return at a Glance

Keep this small table near you until it becomes second nature.

Question print return
Who is it for? The person reading the screen The program that called the function
Can you store the result? No, you get None Yes, in a variable
Can you do more math with it? No Yes
Does it stop the function? No Yes, right away

⚠️ Common Mistakes

A few traps catch people again and again. Watch for these.

  • Using print when you meant return. The number shows on screen but your variable gets None.
# ❌ Avoid: prints the total but hands back nothing
def total_price(quantity, unit_price):
print(quantity * unit_price)
# βœ… Good: hands the total back so the caller can use it
def total_price(quantity, unit_price):
return quantity * unit_price
  • Forgetting to catch the value. If you call a function that returns something but never store it, that value just disappears.
# ❌ Avoid: the returned total is thrown away
total_price(3, 250)
# βœ… Good: catch it in a variable
cart_total = total_price(3, 250)
  • Writing code after return in the same block. Once return runs, the function stops. Lines below it never happen.
# ❌ Avoid: the print never runs
def add(a, b):
return a + b
print("done") # unreachable, Python never gets here
  • Mixing them up in your head. return does not show anything on screen. If you want to see the value too, you still need a print at the caller.

βœ… Best Practices

Small habits that keep your functions clean and easy to trust.

  • Let a function do its math and return the result. Let the caller decide whether to print it, save it, or send it somewhere else.
  • Return a real value, not a printed message, whenever the result might be used again.
  • Give the returned thing a clear home with a well-named variable, like cart_total or final_amount, not just x.
  • If a function is only meant to display something, it is fine to use print and let it return None. Just know that is what is happening.

🧩 What You’ve Learned

βœ… return sends a value back from a function to the caller, so you can store it and reuse it.

βœ… print only shows text on the screen. It does not give your code a value to keep.

βœ… The returned value takes the place of the function call, so you can do more math with it right away.

βœ… A function with no return (or a bare return) gives back None, which means β€œno real value.”

βœ… A real function like total_price returns a clean result that the rest of your program can build on.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What does return do that print does not?

    Why: return sends a value back to the calling code, which can then store or reuse it; print only displays text.

  2. 2

    What is stored in result after: result = greet("Alex"), where greet only uses print and has no return?

    Why: A function with no return statement gives back None, so result holds None.

  3. 3

    Given def add(a, b): return a + b, what is the value of add(10, 5) + add(2, 2)?

    Why: Each call returns a number (15 and 4), and those numbers are added to give 19.

  4. 4

    What happens to any code written on the line after a return runs in the same block?

    Why: return immediately ends the function, so any code below it in that block is never reached.

πŸš€ What’s Next?

You can now hand values back from your functions and reuse them. Next we make functions friendlier by giving arguments a fallback value. That way callers do not always have to pass everything.

Default Parameters

Share & Connect