Bird
Raised Fist0
Pythonprogramming~10 mins

Return values in Python - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Return values
Function called
Execute function code
Return value reached?
NoContinue executing
Yes
Send value back to caller
Function ends
When a function is called, it runs its code until it hits a return statement, then sends back a value and stops.
Execution Sample
Python
def add(a, b):
    return a + b

result = add(3, 4)
print(result)
This code defines a function that adds two numbers and returns the sum, then prints the result.
Execution Table
StepActionEvaluationResult
1Call add(3, 4)Parameters a=3, b=4Function starts
2Calculate a + b3 + 47
3Return 7Return value reached7 sent back to caller
4Assign result = 7Store returned valueresult = 7
5Print resultPrint 7Output: 7
💡 Function ends after return statement sends value back
Variable Tracker
VariableStartAfter Step 3After Step 4Final
aundefined3undefinedundefined
bundefined4undefinedundefined
resultundefinedundefined77
Key Moments - 3 Insights
Why does the function stop running after the return statement?
Because the return statement sends a value back and immediately ends the function, as shown in step 3 of the execution_table.
What happens if there is code after the return statement?
That code does not run because the function already ended at the return, as seen in the execution flow where return ends the function.
How does the returned value get used outside the function?
The returned value is assigned to a variable (result) outside the function, shown in step 4 where result gets the value 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'result' after step 4?
Aundefined
B7
C3
DNone
💡 Hint
Check the 'result' variable in variable_tracker after step 4
At which step does the function send the value back to the caller?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look for the step where 'Return value reached' happens in execution_table
If the return statement was removed, what would happen to the output?
AOutput would be 7
BOutput would be an error
COutput would be None
DOutput would be 0
💡 Hint
Without return, function returns None by default, so result would be None
Concept Snapshot
def function_name(params):
    return value

- 'return' sends a value back to where the function was called
- Function stops running after return
- Returned value can be stored or used outside
- If no return, function returns None by default
Full Transcript
When you call a function, it runs its code. When it reaches a return statement, it sends a value back to the caller and stops running. This returned value can be saved in a variable or used directly. If the function has no return statement, it returns None automatically. For example, a function that adds two numbers returns their sum with return a + b. The program then can print or use that sum. The return statement is important because it ends the function and gives back a result.

Practice

(1/5)
1. What does the return statement do in a Python function?
easy
A. It sends a value back to where the function was called.
B. It prints a value to the screen.
C. It creates a new variable inside the function.
D. It pauses the function without sending any value.

Solution

  1. Step 1: Understand the purpose of return

    The return statement ends the function and sends a value back to the caller.
  2. Step 2: Differentiate from printing or pausing

    Printing shows output but does not send a value back; pausing does not return a value.
  3. Final Answer:

    It sends a value back to where the function was called. -> Option A
  4. Quick 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
A. return(10,)
B. return 10
C. return = 10
D. return: 10

Solution

  1. Step 1: Recall correct return syntax

    The correct syntax is simply return followed by the value without assignment or colon.
  2. Step 2: Check each option

    return 10 is correct; return(10,) returns a tuple (10,) due to the trailing comma; the others cause syntax errors.
  3. Final Answer:

    return 10 -> Option B
  4. Quick 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
A. None
B. 34
C. 7
D. Error

Solution

  1. Step 1: Understand the function behavior

    The function add returns the sum of a and b.
  2. Step 2: Calculate the return value

    Calling add(3, 4) returns 7, which is stored in result and printed.
  3. Final Answer:

    7 -> Option C
  4. Quick 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
A. Add return x, y instead of product.
B. Change print(product) to return print(product).
C. Remove product and just print x * y.
D. Add return product to send the result back.

Solution

  1. Step 1: Identify the problem

    The function prints the product but does not return it, so no value is sent back.
  2. Step 2: Fix by returning the product

    Adding return product sends the result back to the caller.
  3. Final Answer:

    Add return product to send the result back. -> Option D
  4. Quick 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
A. def first_non_empty(strings): for s in strings: if s: return s return None
B. def first_non_empty(strings): for s in strings: if s == '': return s return None
C. def first_non_empty(strings): for s in strings: if not s: return s return None
D. def first_non_empty(strings): for s in strings: return s return None

Solution

  1. Step 1: Understand the goal

    The function should return the first string that is not empty (truthy) or None if none found.
  2. Step 2: Analyze each option

    The version with if not s: return s returns the first falsy (empty) string; if s == '' returns the first exactly empty string; if s: return s returns the first truthy (non-empty) string; the version without if returns the first string regardless. Only the truthy check is correct.
  3. Final Answer:

    def first_non_empty(strings): for s in strings: if s: return s return None -> Option A
  4. Quick 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