Chapter 5: Functions / Lesson 27

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:

basic_return.py
# Function that returns a value def add(a, b): return a + b # Store the returned value result = add(5, 3) print(result) # Output: 8 # Use the returned value directly print(add(10, 20)) # Output: 30 # Use in expressions total = add(1, 2) + add(3, 4) print(total) # Output: 10

Return vs Print

It's important to understand the difference between return and print:

return_vs_print.py
# Function that prints (no return) def calculate_print(a, b): print(a + b) # Just displays, doesn't return # Function that returns def calculate_return(a, b): return a + b # Returns the value # Difference in usage result1 = calculate_print(5, 3) # Prints 8, but result1 is None result2 = calculate_return(5, 3) # result2 is 8 print(f"Result1: {result1}") # Output: Result1: None print(f"Result2: {result2}") # Output: Result2: 8

💡 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):

multiple_returns.py
# Function returning multiple values def get_name_and_age(): return "Alice", 25 # Returns a tuple # Unpack the returned values name, age = get_name_and_age() print(f"{name} is {age} years old") # Output: Alice is 25 years old # Or use as a single tuple info = get_name_and_age() print(info) # Output: ('Alice', 25) # Return multiple calculated values def calculate(a, b): return a + b, a - b, a * b, a / b sum_val, diff, prod, quot = calculate(10, 5) print(f"Sum: {sum_val}, Difference: {diff}, Product: {prod}, Quotient: {quot}")

Conditional Returns

Functions can return different values based on conditions:

conditional_return.py
# Function with conditional returns def get_grade(score): if score >= 90: return "A" elif score >= 80: return "B" elif score >= 70: return "C" else: return "F" print(get_grade(95)) # Output: A print(get_grade(75)) # Output: C # Check if number is even def is_even(num): return num % 2 == 0 # Returns True or False print(is_even(4)) # Output: True print(is_even(7)) # Output: False

Early Returns

The return statement immediately exits the function, which is useful for early exits:

early_return.py
# Early return for validation def divide(a, b): if b == 0: return "Cannot divide by zero" # Early exit return a / b # Only reached if b != 0 print(divide(10, 2)) # Output: 5.0 print(divide(10, 0)) # Output: Cannot divide by zero # Find first positive number def find_positive(numbers): for num in numbers: if num > 0: return num # Return immediately when found return None # If no positive number found print(find_positive([-1, -2, 3, -4])) # Output: 3

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

🎉

Lesson Complete!

Great work! Continue to the next lesson.

main.py
📤 Output
Click "Run" to execute...