Have you ever written a Python function that calculates a perfect result, but then you can’t use that result anywhere else in your code? You find yourself stuck because the answer is just printed to the screen and trapped inside the function. If this sounds familiar, you’re not alone. This is one of the most common “aha!” moments for beginners learning about functions.

The solution, and the key to unlocking the true power of functions, is the return statement. In this guide, we’ll demystify the Python return statement. You’ll learn what it is, why it’s crucial, and how to use it to write functions that don’t just do things, but give things back, making your code flexible and powerful. By the end, you’ll be able to transform your basic functions into professional, reusable building blocks for your programs.
What is a Python Return Statement?
At its core, a function is a machine: you put inputs in, it performs a task, and it gives you an output. The return statement is how a function delivers that output back to the line of code that called it.
Without a return statement, any value created inside a function is essentially lost when the function finishes running. The function might print the value to your console, but your main program can’t capture and use that value for other calculations, assignments, or decisions.
Let’s look at the problem from the video.
The Problem: Functions That Print But Don’t Return
Imagine a simple function designed to add two numbers.
# A function that prints but does not return
def sum(x, y):
result = x + y
print(result) # This only displays the value
# Let's try to use the result
x = sum(4, 5)
print("The value of x is:", x)If you run this code, you’ll see two lines of output:
9 The value of x is: None
The function correctly calculated 4 + 5 = 9 and printed it. However, when we tried to assign sum(4, 5) to the variable x, the value of x became None. This is because the function didn’t return anything; it just printed the value and ended. None is Python’s way of saying “nothing here.”
The Solution: Using Return to Send a Value Back
Now, let’s fix the function by replacing print with return.
# A function that returns a value
def sum(x, y):
result = x + y
return result # This sends the value back to the caller
# Now let's use the result
x = sum(4, 5)
print("The value of x is:", x)Now, the output is:
The value of x is: 9
Magic! The function call sum(4, 5) is now replaced by the value it returns, which is 9. This allows us to store it in a variable, print it, or use it in further calculations.
Why is the Return Statement So Important?
The return statement transforms functions from isolated scripts into interactive tools. Here’s why it’s a game-changer:
- Reusability: A returned value can be used anywhere in your code.
- Modularity: You can break down complex problems into small functions that return specific results.
- Testability: It’s easy to check if a function returns the expected output for a given input.
A Practical Example: Using Returned Values in Expressions
The real power shines when you use returned values directly in expressions. Let’s revisit the example from the video.
def sum(x, y):
return x + y
# Using the returned values in an expression
total = sum(4, 5) + sum(9, 7)
print("The total is:", total)What happens step-by-step:
sum(4, 5)is called, which returns9.sum(9, 7)is called, which returns16.- The expression now becomes
9 + 16. - The result,
25, is assigned to the variabletotal.
Without the return statement, this would have caused a TypeError because you can’t add two None values together.
Creating Functions That Make Decisions
Another powerful use of return is in functions that check a condition and return a Boolean value (True or False). This is extremely useful for controlling the flow of your program.
Let’s convert a function that checks if a number is even.
# A function that returns True or False
def is_even(number):
if number % 2 == 0:
return True
else:
return False
# Using the returned Boolean in an if-statement
if is_even(8):
print("Even number")
else:
print("Odd number")In this code:
is_even(8)is evaluated. Since 8 is divisible by 2, the function returnsTrue.- The
ifstatement seesif True:, so it executes the first block and prints “Even number”.
This pattern is clean, readable, and highly effective.
Ready to Go from Basics to Pro?
Understanding the return statement is a massive leap forward in your Python journey. You’re now moving from writing simple scripts to crafting modular, professional-grade code. But this is just the beginning. To truly master Python, you need a structured path that covers everything from core syntax to building real-world applications.
Our comprehensive course is designed to take you from beginner to pro with expert guidance, hands-on projects, and a supportive community. You’ll solidify your fundamentals and learn how to piece concepts like the return statement together to create complex, useful programs.
If you’re serious about mastering Python, check out our pre-recorded python comprehensive video course.
Conclusion
The Python return statement is your key to creating truly useful and powerful functions. Remember, a function that prints is like a chef who shows you a finished dish; a function that returns is like a chef who hands you the dish so you can use it however you like. You’ve learned the critical difference between printing and returning, how to capture returned values in variables, and how to use them in expressions and logical decisions. Keep practicing by writing your own functions that take inputs and return outputs—it’s the best way to cement this essential concept.
Common Questions About the Return Statement
Can a function have multiple return statements?
Yes! A function can have multiple return statements, but only the first one that is executed will end the function. This is common in conditional blocks (like if/else), as we saw in the is_even function. As soon as a return is hit, the function exits.
What happens if a function has no return statement?
If a function doesn’t have a return statement, or if the return statement doesn’t specify a value, the function automatically returns None. This is the default behavior.
Can a function return more than one value?
Yes, a function can return multiple values by separating them with commas. Python will package them into a tuple. You can then unpack them into separate variables.
def get_user_info():
name = "Alice"
age = 30
return name, age # Returns a tuple: ('Alice', 30)
user_name, user_age = get_user_info()














