Python Return Values
Table of Contents + β
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.returnsends 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
7result is: NoneThe 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: 7See 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 + bsends back7. - That
7takes the place ofadd(3, 4). - So the line becomes
result = 7. Nowresultholds7.
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
19You 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_pricetakes how many items and the price of one item.- Inside, it multiplies them and stores the result in
total. return totalhands that number back to the caller.cart_totalcatches it. So now the total lives in a variable we own.
Output
Items total: 750Now 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 = 40tax = cart_total * 0.05
final_amount = cart_total + delivery + taxprint("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.5This 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 Alexanswer holds: NoneThe 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 | 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
printwhen you meantreturn. The number shows on screen but your variable getsNone.
# β Avoid: prints the total but hands back nothingdef total_price(quantity, unit_price): print(quantity * unit_price)
# β
Good: hands the total back so the caller can use itdef 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 awaytotal_price(3, 250)
# β
Good: catch it in a variablecart_total = total_price(3, 250)- Writing code after
returnin the same block. Oncereturnruns, the function stops. Lines below it never happen.
# β Avoid: the print never runsdef add(a, b): return a + b print("done") # unreachable, Python never gets here- Mixing them up in your head.
returndoes not show anything on screen. If you want to see the value too, you still need aprintat the caller.
β Best Practices
Small habits that keep your functions clean and easy to trust.
- Let a function do its math and
returnthe 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_totalorfinal_amount, not justx. - If a function is only meant to display something, it is fine to use
printand let it returnNone. 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
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
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
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
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.