Returning Results with return
π·οΈ Functions / Return Values
π§ Context Introduction
Functions are powerful because they can take inputs, perform actions, and then send back a result for you to use elsewhere in your code. Without returning a value, a function simply runs its instructions and disappearsβleaving nothing behind for you to capture. The return statement is how you hand a result back to the part of your program that called the function.
βοΈ What Does return Do?
When you use return inside a function, two things happen:
- The function stops executing immediately (any code after return is ignored)
- The value after return is sent back to the caller
You can then store that returned value in a variable, use it in a calculation, or pass it to another function.
π οΈ Basic Example of Returning a Value
Consider a function that calculates the square of a number:
- Define the function: def square(number):
- Inside the function: result = number * number
- Return the result: return result
- Call the function and store the output: output = square(5)
- Now output holds the value 25
Without the return statement, the variable output would contain None instead of the calculated result.
π Comparison: Function With vs Without return
| Aspect | With return |
Without return |
|---|---|---|
| What the caller gets | The actual computed value | None (nothing useful) |
| Can store the result | Yes, in a variable | No, variable becomes None |
| Can use result in expressions | Yes, e.g., total = add(3, 4) * 2 | No, you lose the computation |
| Function purpose | To compute and deliver a value | To perform an action (like printing) |
π΅οΈ Returning Multiple Values
Python allows you to return more than one value at once using a comma-separated list:
- return x, y, z sends back a tuple containing all three values
- You can unpack them directly: a, b, c = my_function()
- Example: def min_max(list): returns return min_val, max_val
This is very useful when you need a function to produce multiple related results.
π§ͺ Common Patterns with return
Early return for validation: - Check if input is valid first - If invalid, return None or an error message immediately - If valid, continue with the main logic and return the result
Returning computed results: - Perform calculations or data transformations inside the function - Use return to hand back the final computed value
Returning Boolean values: - Functions that check conditions often return True or False - Example: def is_even(number): returns return number % 2 == 0
β Key Takeaways
- return sends a value back from a function to where it was called
- A function without return gives you None
- You can return one value, multiple values (as a tuple), or nothing at all
- Code after return inside the same function block is never executed
- Use return when you need to capture and reuse a function's output elsewhere in your program
The return statement sends a value back from a function to the code that called it, allowing the function's result to be stored or used elsewhere.
π§ Example 1: Returning a simple value
This example shows a function that returns a single number.
def get_pi():
return 3.14159
result = get_pi()
print(result)
π€ Output: 3.14159
π§ Example 2: Returning a computed result
This example shows a function that calculates and returns a value based on its input.
def square(number):
result = number * number
return result
output = square(5)
print(output)
π€ Output: 25
π§ Example 3: Returning a string from a function
This example shows a function that builds and returns a formatted message.
def create_greeting(name):
message = "Hello, " + name + "!"
return message
greeting = create_greeting("Engineer")
print(greeting)
π€ Output: Hello, Engineer!
π§ Example 4: Returning a boolean (True/False)
This example shows a function that checks a condition and returns a logical result.
def is_even(number):
remainder = number % 2
result = (remainder == 0)
return result
check = is_even(8)
print(check)
π€ Output: True
π§ Example 5: Using a returned value in another calculation
This example shows how to chain functions by using one function's return value as input to another.
def add_ten(value):
new_value = value + 10
return new_value
def multiply_by_two(value):
new_value = value * 2
return new_value
first_result = add_ten(5)
second_result = multiply_by_two(first_result)
print(second_result)
π€ Output: 30
π§ Example 6: Returning early from a function
This example shows how return can stop a function immediately and send a value back.
def check_positive(number):
if number < 0:
return "Negative number detected"
return "Number is positive"
result1 = check_positive(-3)
result2 = check_positive(7)
print(result1)
print(result2)
π€ Output: Negative number detected
π€ Output: Number is positive
Comparison Table: Common Return Patterns
| Pattern | Description | Example Return Value |
|---|---|---|
| Single value | Returns one number or string | return 42 |
| Computed result | Returns result of an expression | return a + b |
| Conditional return | Returns different values based on logic | return "yes" or return "no" |
| Early return | Exits function before reaching the end | return None |
| Chained return | Return value used as input to another function | func2(func1(x)) |
π§ Context Introduction
Functions are powerful because they can take inputs, perform actions, and then send back a result for you to use elsewhere in your code. Without returning a value, a function simply runs its instructions and disappearsβleaving nothing behind for you to capture. The return statement is how you hand a result back to the part of your program that called the function.
βοΈ What Does return Do?
When you use return inside a function, two things happen:
- The function stops executing immediately (any code after return is ignored)
- The value after return is sent back to the caller
You can then store that returned value in a variable, use it in a calculation, or pass it to another function.
π οΈ Basic Example of Returning a Value
Consider a function that calculates the square of a number:
- Define the function: def square(number):
- Inside the function: result = number * number
- Return the result: return result
- Call the function and store the output: output = square(5)
- Now output holds the value 25
Without the return statement, the variable output would contain None instead of the calculated result.
π Comparison: Function With vs Without return
| Aspect | With return |
Without return |
|---|---|---|
| What the caller gets | The actual computed value | None (nothing useful) |
| Can store the result | Yes, in a variable | No, variable becomes None |
| Can use result in expressions | Yes, e.g., total = add(3, 4) * 2 | No, you lose the computation |
| Function purpose | To compute and deliver a value | To perform an action (like printing) |
π΅οΈ Returning Multiple Values
Python allows you to return more than one value at once using a comma-separated list:
- return x, y, z sends back a tuple containing all three values
- You can unpack them directly: a, b, c = my_function()
- Example: def min_max(list): returns return min_val, max_val
This is very useful when you need a function to produce multiple related results.
π§ͺ Common Patterns with return
Early return for validation: - Check if input is valid first - If invalid, return None or an error message immediately - If valid, continue with the main logic and return the result
Returning computed results: - Perform calculations or data transformations inside the function - Use return to hand back the final computed value
Returning Boolean values: - Functions that check conditions often return True or False - Example: def is_even(number): returns return number % 2 == 0
β Key Takeaways
- return sends a value back from a function to where it was called
- A function without return gives you None
- You can return one value, multiple values (as a tuple), or nothing at all
- Code after return inside the same function block is never executed
- Use return when you need to capture and reuse a function's output elsewhere in your program
Interactive Views
You are currently in π All-in-One mode. Use the tabs at the top to switch to π Theory Only or π» Code Only views.
The return statement sends a value back from a function to the code that called it, allowing the function's result to be stored or used elsewhere.
π§ Example 1: Returning a simple value
This example shows a function that returns a single number.
def get_pi():
return 3.14159
result = get_pi()
print(result)
π€ Output: 3.14159
π§ Example 2: Returning a computed result
This example shows a function that calculates and returns a value based on its input.
def square(number):
result = number * number
return result
output = square(5)
print(output)
π€ Output: 25
π§ Example 3: Returning a string from a function
This example shows a function that builds and returns a formatted message.
def create_greeting(name):
message = "Hello, " + name + "!"
return message
greeting = create_greeting("Engineer")
print(greeting)
π€ Output: Hello, Engineer!
π§ Example 4: Returning a boolean (True/False)
This example shows a function that checks a condition and returns a logical result.
def is_even(number):
remainder = number % 2
result = (remainder == 0)
return result
check = is_even(8)
print(check)
π€ Output: True
π§ Example 5: Using a returned value in another calculation
This example shows how to chain functions by using one function's return value as input to another.
def add_ten(value):
new_value = value + 10
return new_value
def multiply_by_two(value):
new_value = value * 2
return new_value
first_result = add_ten(5)
second_result = multiply_by_two(first_result)
print(second_result)
π€ Output: 30
π§ Example 6: Returning early from a function
This example shows how return can stop a function immediately and send a value back.
def check_positive(number):
if number < 0:
return "Negative number detected"
return "Number is positive"
result1 = check_positive(-3)
result2 = check_positive(7)
print(result1)
print(result2)
π€ Output: Negative number detected
π€ Output: Number is positive
Comparison Table: Common Return Patterns
| Pattern | Description | Example Return Value |
|---|---|---|
| Single value | Returns one number or string | return 42 |
| Computed result | Returns result of an expression | return a + b |
| Conditional return | Returns different values based on logic | return "yes" or return "no" |
| Early return | Exits function before reaching the end | return None |
| Chained return | Return value used as input to another function | func2(func1(x)) |