The return statement in Python is used to send a value from a function back to the caller, ending the function’s execution.
What is the Python return statement?
Key takeaways:
The
returnstatement delivers function results. Withoutreturn, a function performs its task but does not send back a result, similar to a chef preparing food but not serving it.Every function returns something. If no
returnstatement is used, Python automatically returnsNone.
returncan send multiple values. Unlike some other languages, Python allows returning multiple values as tuples.
returncan exit a function early. A function stops executing as soon as it encounters areturnstatement.Using
returnmakes functions reusable and efficient. It enables further data processing, making Python functions more useful and flexible.
Assume you are a chef in a kitchen. You prepare a delicious meal, but instead of serving it to the customer, you just leave it on the counter and walk away. The customer never gets their food!
That’s exactly what happens if you don’t use return in a function. The function does its job, but if it doesn’t return anything, the result is lost. The return statement is like the waiter who takes the meal from the kitchen and delivers it to the customer.
Without return, a function is just doing work without giving you the result. That’s like solving a math problem but never writing down the answer! So, if we want our functions to actually produce useful results, we need return.
Syntax of the return statement
def function_name():return value
function_nameis the name of the function.return valuespecifies what the function will send back when called.
How does the return statement work
Every function in Python returns a value, even if no explicit return statement is provided. If a function does not have a return statement, it automatically returns None.
def greet():print("Hello!")result = greet()print(result) # Output: None
Since there is no return statement, the function implicitly returns None. Now, let’s use the return statement.
A return statement consists of the return keyword followed by an expression or value.
def test_func():a = 10 + 1return aprint(test_func());
Every function in Python returns something. A function without a return statement returns None. Something similar can be said about functions that contain a return statement but no return value.
def test_func(): #Function without return statementa = 10b = 4c = a + bdef test_func2(): #Function without return valuea = 10returnprint(test_func())print(test_func2())
Returning multiple values
Python enables returning multiple values as a tuple, list, or dictionary. This feature is particularly useful when a function needs to provide multiple pieces of related information.
def test_func():a = 10 + 1b = "eleven"return a, bx, y = test_func()print(x)print(y)
Using return to exit a function early
We can use return to stop a function before it completes.
def check_age(age):if age < 18:return "You are underage."return "You are an adult."print(check_age(16)) # Output: You are underage.print(check_age(21)) # Output: You are an adult.
Become a Python developer with our comprehensive learning path!
Ready to kickstart your career as a Python Developer? Our “Become a Python Developer” path is designed to take you from your first line of code to landing your first job. This comprehensive journey offers essential knowledge, hands-on practice, interview preparation, and a mock interview, ensuring you gain practical, real-world coding skills. With our AI mentor by your side, you’ll overcome challenges with personalized support, building the confidence needed to excel in the tech industry.
Conclusion
The return statement is a fundamental part of Python functions. It allows functions to send values back to the caller, making them reusable and efficient. Without return, functions can only perform actions but cannot pass back data for further processing.
Frequently asked questions
Haven’t found what you were looking for? Contact Us
What is the `return` statement?
What does `get()` return in Python?
What is the return symbol in Python?
What does `return all` do in Python?
Why do we use `return`?
Does `return` print in Python?
Free Resources