‘return’ Statement in Python
rajneesh
3 min read
- python
when you are learning python you can see return statement sometime with or without parameters inside the function block. are you thinking why it is use. i will example you in very simple terms, the return statement is like a messenger. It takes the result from a function and delivers it back to the part of your code where function is called. i will 100% sure if you read this complete post you can completely understand return statement. for this knowledge i have read python official docs Twice for better understanding by this knowledge i will explain this topic in very easy manner with examples. let start learn,
What is a ‘return’ Statement?
Imagine you are asked your friend to calculate the total cost of your shopping list. Your friend does the that calculation and then returns the total value they get after calculating to you. like that In Python, the return
statement does something similar. When a function completes its task, it return
back and give the result where function is called.
def add_numbers(a, b):
result = a + b
return result
total = add_numbers(11, 31)
print(total) # Output: 42
In this example, add_numbers
is a function that adds two numbers. The return
statement sends the sum (result
) back to where the function was called, and then print the value of total.
Why Use ‘return’?
Using return
has several benefits:
Control: You decide what the function outputs.
Flexibility: You can return any Python object, if it a number, a string, or even another function!
Clarity: It makes your code easier to understand and maintain.
Returning Multiple Values
One of the cool things about Python is that you can return more than one value using tuples, lists, or dictionaries.
def get_user_info():
name = "Alex"
age = 30
return name, age # Returning a tuple
user_name, user_age = get_user_info()
print(user_name, user_age) # Output: Alex 30
print(type(get_user_info())) # Output: <class 'tuple'>
Here, get_user_info
returns both name and age, which we store in user_name
and user_age after that we can print both values you can also check type of return value it must be tuple
.
When ‘return’ is Absent
If you don’t include a return
statement, Python implicitly returns None
. This is Python’s way of saying “nothing” or “empty”.
def say_hello():
print("Hello, my friends hello")
result = say_hello()
print(result) # Output: None
if we print say_hello
return value, it doesn’t return anything, so result
is None
.
Best Practices
Use
return
to make your functions more predictable and testable.Keep your functions focused on a single task for clearer and more efficient
return
statements.conclusion
the
return
statement is a powerful feature in Python that helps you manage what your functions output. and its make you written code cleaner, more efficient. in this post i have not cover examples of different type of value because you can try by user self for better understanding by return list, dictionaries etc. its increase understanding of how return statement work. if you think you need more examples for this topic you can comment i will provide you one more post with all type of examples. if you like this post please like share and follow me on this website.