Return Keyword
Understanding the Return Statement
The return keyword allows functions to send values back to the caller. While functions can perform actions (like printing), returning values makes functions reusable and allows you to store or use the result in other parts of your code.
Functions without return implicitly return None. Using return explicitly sends a value back to where the function was called.
Basic Return Statement
The simplest use of return sends a single value back:
Return vs Print
It's important to understand the difference between return and print:
💡 Key Difference
print() displays output but doesn't return a usable value. return sends a value back that you can store, use in calculations, or pass to other functions.
Multiple Return Values
Python functions can return multiple values using tuples (which are automatically unpacked):
Conditional Returns
Functions can return different values based on conditions:
Early Returns
The return statement immediately exits the function, which is useful for early exits:
Best Practices
✅ When to Use Return
• When you need to use the result in calculations
• When you want to store the result for later use
• When passing values to other functions
• For functions that compute and provide data
💡 Important Notes
• Functions without return return None
• return immediately exits the function
• You can return any data type: numbers, strings, lists, tuples, etc.
• Multiple values are returned as tuples