Return values let a function send back a result after it finishes. This helps you reuse the result later in your program.
Return values in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
def function_name(parameters): # do something return value
The return keyword sends a value back to where the function was called.
Once return runs, the function stops running any more code inside it.
Examples
Python
def add(a, b): return a + b
Python
def greet(): return "Hello!"
True if a number is even, otherwise False.Python
def check_even(num): if num % 2 == 0: return True else: return False
Sample Program
This program defines a function that multiplies two numbers and returns the result. Then it prints the result.
Python
def multiply(x, y): return x * y result = multiply(4, 5) print(f"4 times 5 is {result}")
Important Notes
If a function does not have a return statement, it returns None by default.
You can return any type of value: numbers, text, lists, or even other functions.
Use return to get results out of functions instead of printing inside them for better reuse.
Summary
Return values let functions send results back to the caller.
Use return to stop a function and give back a value.
Returned values can be saved, printed, or used in other calculations.
Practice
1. What does the
return statement do in a Python function?easy
Solution
Step 1: Understand the purpose of
Thereturnreturnstatement ends the function and sends a value back to the caller.Step 2: Differentiate from printing or pausing
Printing shows output but does not send a value back; pausing does not return a value.Final Answer:
It sends a value back to where the function was called. -> Option AQuick Check:
Return sends value back [OK]
Hint: Return sends value back, print shows output only [OK]
Common Mistakes:
- Confusing return with print
- Thinking return pauses without value
- Assuming return creates variables
2. Which of the following is the correct syntax to return the value 10 from a function?
easy
Solution
Step 1: Recall correct return syntax
The correct syntax is simplyreturnfollowed by the value without assignment or colon.Step 2: Check each option
return 10is correct;return(10,)returns a tuple (10,) due to the trailing comma; the others cause syntax errors.Final Answer:
return 10 -> Option BQuick Check:
Return value with 'return value' syntax [OK]
Hint: Use 'return value' without assignment or colon [OK]
Common Mistakes:
- Using assignment with return
- Adding colon after return
- Using parentheses incorrectly
3. What is the output of this code?
def add(a, b):
return a + b
result = add(3, 4)
print(result)medium
Solution
Step 1: Understand the function behavior
The functionaddreturns the sum ofaandb.Step 2: Calculate the return value
Callingadd(3, 4)returns 7, which is stored inresultand printed.Final Answer:
7 -> Option CQuick Check:
3 + 4 = 7 [OK]
Hint: Return value is sum, printed as 7 [OK]
Common Mistakes:
- Concatenating numbers as strings
- Forgetting to return value
- Expecting print inside function
4. Find the error in this function and choose the correct fix:
def multiply(x, y):
product = x * y
print(product)medium
Solution
Step 1: Identify the problem
The function prints the product but does not return it, so no value is sent back.Step 2: Fix by returning the product
Addingreturn productsends the result back to the caller.Final Answer:
Addreturn productto send the result back. -> Option DQuick Check:
Return value to send result back [OK]
Hint: Return value to send it back, print only shows output [OK]
Common Mistakes:
- Returning print() instead of value
- Not returning any value
- Returning wrong variables
5. You want a function that returns the first non-empty string from a list or
None if all are empty. Which function correctly does this?hard
Solution
Step 1: Understand the goal
The function should return the first string that is not empty (truthy) or None if none found.Step 2: Analyze each option
The version withif not s: return sreturns the first falsy (empty) string;if s == ''returns the first exactly empty string;if s: return sreturns the first truthy (non-empty) string; the version withoutifreturns the first string regardless. Only the truthy check is correct.Final Answer:
def first_non_empty(strings): for s in strings: if s: return s return None -> Option AQuick Check:
Return first truthy string or None [OK]
Hint: Return first truthy value, else None [OK]
Common Mistakes:
- Returning empty strings instead of non-empty
- Returning on first loop without condition
- Confusing truthy and falsy values
