Python From Scratch #34 - The return Statement
Python From Scratch #34 - The return Statement
Learn how to return values from functions and use those values in your programs.
Hey 👋
Welcome back to the Python from Scratch series.
In lesson #31, we saw that a function without a return statement gives back None.
Now let’s learn how to make a function return a value.
What Does return Do?
The return statement sends a value back from a function.
For example:
def add(a, b):
return a + bWhen we call the function:
result = add(5, 3)
print(result)
# Output:
# 8The function calculates 5 + 3 and returns the result.
That returned value is then stored in result.
You can think of it like this:
Function → does some work → returns a value.
return vs print()
This is an important difference.
Consider this function:
def add(a, b):
print(a + b)If we call it:
result = add(5, 3)It prints 8.
But result contains None.
Why?
Because print() displays a value. It doesn’t return that value from the function.
Now compare it with:
def add(a, b):
return a + bWhen we do:
result = add(5, 3)result contains 8.
So:
print()displays a value.returngives a value back from a function.
Using the Returned Value
One of the useful things about return is that you can store the returned value and use it later.
def add(a, b):
return a + b
result = add(5, 3)
print(result)
print(result * 2)
# Output:
# 8
# 16The function returns 8. We store it in result and then we can use result just like any other value.
Returning Different Values
A function can return different values depending on a condition.
For example:
def check_age(age):
if age >= 18:
return "Adult"
else:
return "Minor"Now:
result = check_age(20)
print(result)
# Output:
# AdultAnd:
result = check_age(15)
print(result)
# Output:
# MinorThe function returns a different value depending on the condition.
Returning Multiple Values
A function can also return more than one value at once.
For example:
def get_name_and_age():
return "Shefali", 27
name, age = get_name_and_age()
print(name)
print(age)
# Output:
# Shefali
# 27Here, the function returns two values: return "Shefali", 27
Python allows us to assign those values directly to two variables: name, age = get_name_and_age()
This is similar to the multiple variable assignment we saw earlier in the series.
Python actually groups the returned values together as a tuple. We’ll learn more about tuples later.
For now, just remember that you can return multiple values and assign them to separate variables.
return Stops the Function
When Python reaches a return statement, the function immediately stops running.
For example:
def greet():
print("Hello")
return
print("This won't run")
greet()
# Output:
# HelloThe second print() never runs because return ended the function.
You can also return a value:
def greet():
print("Hello")
return "Done"
print("This won't run")Once Python reaches return "Done", the function stops.
Code placed after a return statement is called unreachable code.
Python won’t raise an error for it, but most code editors will warn you about it because that code can never run.
Returning More Complex Values
A function doesn’t have to return just numbers.
It can return strings and other values you’ve already learned about.
For example:
def get_name():
return "Shefali"
name = get_name()
print(name)
# Output:
# ShefaliYou can also return a result from a calculation:
def square(number):
return number * number
result = square(5)
print(result)
# Output:
# 25Returning Early
You can also use return to stop a function early.
For example:
def check_number(number):
if number < 0:
return "Negative number"
return "Positive number"If we call:
print(check_number(-5))
# Output:
# Negative numberWhen number < 0 is True, the first return runs and the function ends.
The second return isn’t reached.
One Function Can Return a Value to Another Part of Your Program
This is where functions become really useful.
For example:
def calculate_total(price, quantity):
return price * quantity
total = calculate_total(100, 3)
print("Total:", total)
# Output:
# Total: 300The function handles the calculation and gives the result back to the rest of the program.
The function doesn’t need to know what you’ll do with the result.
You can print it, store it, or use it in another calculation.
Try It Yourself
Now, a small exercise for you. Try these:
Create a function that accepts two numbers and returns their sum.
Create a function that accepts a name and returns a greeting. Store the returned value in a variable and print it.
Create a function that accepts two numbers and returns the larger number. Use
ifandelseto make the decision.
That’s all for today!
Tomorrow, we’ll learn about variable-length arguments and how *args lets a function accept any number of positional arguments.
If you enjoyed this newsletter, consider sharing it with another developer. And if you have any feedback, I’d love to hear it. It would genuinely mean a lot to me. ❤️
Want to sponsor my work? Let’s talk!
See you tomorrow ✨
- Shefali
Thanks for reading DevBits by Shefali! Subscribe for free to receive new posts and support my work.
If you enjoyed this edition of DevBits, you can support my work here ☕
Your support helps me keep creating more developer tips, UI snippets, and helpful content for the community.
© 2026 Shefali Jangid
548 Market Street PMB 72296, San Francisco, CA 94104