Nearby lessons
71 of 159Python - Return Statement
- Understand what the return statement is and how it terminates a function
- Return values, expressions, strings, booleans and lists from functions
- Compare print() with return and understand None
- Return multiple values from a function
- Receive multiple returned values using tuple unpacking
Return Statement
When a function completes its execution, sometimes it needs to send a result back to the function caller.
To send the result from a function, Python provides the return statement.
The return statement terminates the execution of the function and transfers control back to the calling statement.
If a value is specified after the return keyword, that value is returned to the caller.
Why Do We Need the Return Statement?
Consider a function that adds two numbers.
If the function only prints the result, the result cannot be used elsewhere in the program.
If the function returns the result, it can be stored in a variable, used in calculations, or passed to another function.
Therefore, returning values makes functions more useful and reusable.
Syntax
Syntax Explanation
| Statement | Purpose |
|---|---|
return value |
Returns the specified value to the caller. |
return |
Terminates the function without returning any value. |
Example 1 - Function Returning a Value
Explanation
The function receives two numbers.
It calculates their sum and returns the result.
The returned value is stored in the variable result.
Finally, the value is printed.
Example 2 - Returning an Expression
Explanation
The expression n * n is evaluated first.
The calculated value is then returned to the caller.
Example 3 - Returning a String
Example 4 - Returning a Boolean Value
Example 5 - Returning a List
Example 6 - Function Without Return Statement
Explanation
The function prints the message.
Since there is no return statement, Python automatically returns None.
Therefore, the variable result stores None.
Example 7 - Empty Return Statement
Explanation
When Python encounters the return statement, the function immediately terminates.
The statement after return is never executed.
Flow of Return Statement
| Step | Action |
|---|---|
| 1 | Function is called. |
| 2 | Function executes its statements. |
| 3 | return sends the result back. |
| 4 | Function execution stops. |
| 5 | Control returns to the caller. |
Difference Between print() and return
| print() | return |
|---|---|
| Displays output on the screen. | Returns a value to the caller. |
| Does not terminate the function. | Terminates the function immediately. |
| Cannot be reused directly. | The returned value can be reused anywhere. |
| Mainly used for displaying information. | Mainly used for sending results back. |
Real-World Applications
- Returning calculated values from mathematical functions.
- Returning database query results.
- Returning API responses.
- Returning validation results.
- Returning processed data from helper functions.
Returning Multiple Values from a Function
In the previous section, we learned how a function can return a single value by using the return statement.
Python also allows a function to return multiple values.
This is one of the powerful features of Python because many programming languages can return only one value directly.
When multiple values are returned, Python automatically packs them into a Tuple.
Why Return Multiple Values?
Sometimes a function performs multiple calculations and needs to send several results back to the caller.
For example:
- Returning addition, subtraction, multiplication and division together.
- Returning student name, marks and grade.
- Returning minimum and maximum values.
- Returning multiple statistics from a dataset.
Instead of calling multiple functions, Python allows us to return all results together.
Syntax
Important Point
Although multiple values appear to be returned, Python actually returns a single Tuple.
Python automatically performs Tuple Packing.
Example 1 - Returning Multiple Values
Explanation
The function returns four values.
Python automatically packs these values into a tuple.
Therefore, the variable result stores a tuple.
Tuple Packing
When multiple values are returned, Python automatically creates a tuple.
This process is called Tuple Packing.
Example
Receiving Multiple Returned Values
Instead of storing the returned tuple in a single variable, we can receive each value into separate variables.
This process is called Tuple Unpacking.
Example
Explanation
The returned tuple contains four values.
Each value is automatically assigned to the corresponding variable.
This process is known as Tuple Unpacking.
Example 2 - Returning Student Information
Example 3 - Returning Minimum and Maximum Values
Example 4 - Returning Different Data Types
Example 5 - Returning a Tuple Explicitly
Automatic Tuple Packing
| Return Statement | Python Stores As |
|---|---|
return 10,20 |
(10,20) |
return 10,20,30 |
(10,20,30) |
return "A",100 |
("A",100) |
return True,50.5 |
(True,50.5) |
Difference Between Single Return and Multiple Return
| Single Return | Multiple Return |
|---|---|
| Returns one value. | Returns multiple values. |
| Any data type. | Automatically packed into a tuple. |
| Stored in one variable. | Can be stored in one variable or unpacked into multiple variables. |
Real-World Applications
- Returning multiple calculation results.
- Returning database records.
- Returning API response values.
- Returning student information.
- Returning minimum and maximum values.
- Returning statistical information.
- The return statement sends a result back to the caller and ends the function
- A function without a return statement returns None
- print() only displays output while return makes the result usable in code
- Functions can return multiple values as an automatically packed tuple
- Tuple unpacking receives multiple returned values