Bird
Raised Fist0
Pythonprogramming~10 mins

Methods with 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 - Methods with return values
Define method
Call method
Execute method body
Return value
Receive returned value
Use returned value
This flow shows how a method is defined, called, executes, returns a value, and how that value is used.
Execution Sample
Python
def add(a, b):
    return a + b

result = add(3, 4)
print(result)
This code defines a method that adds two numbers and returns the sum, then prints the result.
Execution Table
StepActionEvaluationResult
1Define method 'add'Store function with parameters a, bMethod ready to call
2Call add(3, 4)Parameters a=3, b=4Enter method body
3Calculate a + b3 + 47
4Return 7Return value sent backMethod call replaced by 7
5Assign result = 7Store 7 in variable 'result'result = 7
6Print resultOutput value of result7
7EndNo more codeProgram stops
💡 Program ends after printing the returned value 7
Variable Tracker
VariableStartAfter Step 4After Step 5Final
aN/A333
bN/A444
resultUndefinedUndefined77
Key Moments - 2 Insights
Why do we need the 'return' statement inside the method?
The 'return' sends the result back to where the method was called, as shown in step 4 of the execution_table. Without it, the method would not give any output to use.
What happens to the method call after it returns a value?
After returning, the method call is replaced by the returned value (step 4). This value can then be assigned or used elsewhere, like in step 5 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 5?
A3
BUndefined
C7
D4
💡 Hint
Check the 'result' variable in variable_tracker after step 5
At which step does the method return its value?
AStep 4
BStep 2
CStep 5
DStep 6
💡 Hint
Look at the 'Return 7' action in the execution_table
If the method did not have a return statement, what would 'result' be after step 5?
A7
BNone
CError
D3
💡 Hint
In Python, a method without return returns None by default
Concept Snapshot
def method_name(params):
    return value

- 'return' sends value back to caller
- Caller can store or use returned value
- Without return, method returns None
- Use returned value to continue program logic
Full Transcript
This lesson shows how methods with return values work in Python. First, a method is defined with parameters. When called, the method runs its code and uses 'return' to send a value back. The returned value replaces the method call and can be stored in a variable or used directly. For example, a method adding two numbers returns their sum, which can then be printed or used elsewhere. The 'return' statement is essential to get output from a method. Without it, the method returns None by default. This flow helps organize code and reuse logic with results.

Practice

(1/5)
1. What does a method with a return statement do in Python?
easy
A. It sends a value back to where the method was called.
B. It prints a value on the screen.
C. It stops the program immediately.
D. It creates a new variable automatically.

Solution

  1. Step 1: Understand the purpose of return

    The return statement sends a value back from the method to the caller.
  2. Step 2: Differentiate from printing or stopping

    Printing shows output but does not send a value back; stopping ends execution.
  3. Final Answer:

    It sends a value back to where the method was called. -> Option A
  4. Quick Check:

    Method return = sends value back [OK]
Hint: Return sends value back, print shows it only [OK]
Common Mistakes:
  • Confusing return with print
  • Thinking return stops the program
  • Believing return creates variables
2. Which of the following is the correct syntax for a method that returns the sum of two numbers a and b?
easy
A. def add(a, b): return a - b
B. def add(a, b): print(a + b)
C. def add(a, b): return a + b
D. def add(a, b): a + b

Solution

  1. Step 1: Identify correct return usage

    The method must use return to send back the sum a + b.
  2. Step 2: Check each option

    The version with return a - b returns the difference. The version with print(a + b) prints but returns None. The version with just a + b lacks return. Only def add(a, b): return a + b correctly returns the sum.
  3. Final Answer:

    def add(a, b): return a + b -> Option C
  4. Quick Check:

    Return sum = def add(a, b): return a + b [OK]
Hint: Return must be followed by value to send back [OK]
Common Mistakes:
  • Using print instead of return
  • Omitting return keyword
  • Returning wrong expression
3. What is the output of this code?
def multiply(x, y):
    return x * y

result = multiply(3, 4)
print(result)
medium
A. 7
B. 12
C. 34
D. None

Solution

  1. Step 1: Understand the method call

    The method multiply returns the product of 3 and 4, which is 12.
  2. Step 2: Print the returned value

    The variable result stores 12, so print(result) outputs 12.
  3. Final Answer:

    12 -> Option B
  4. Quick Check:

    3 * 4 = 12 [OK]
Hint: Multiply inputs, return result, print shows it [OK]
Common Mistakes:
  • Adding instead of multiplying
  • Printing None by missing return
  • Confusing string concatenation with multiplication
4. Find the error in this method and choose the correct fix:
def greet(name):
    print("Hello, " + name)

message = greet("Alice")
print(message)
medium
A. Remove the argument name from the method.
B. Add print before calling greet.
C. Change message to greet in the last print.
D. Change print to return inside the method.

Solution

  1. Step 1: Identify the problem with return value

    The method prints but does not return a value, so message is None.
  2. Step 2: Fix by returning the greeting string

    Replace print with return to send the greeting back.
  3. Final Answer:

    Change print to return inside the method. -> Option D
  4. Quick Check:

    Return greeting to assign message [OK]
Hint: Use return to get value, not print [OK]
Common Mistakes:
  • Expecting print to return a value
  • Removing needed parameters
  • Changing variable names incorrectly
5. You want to write a method that returns a dictionary with keys as numbers from 1 to n and values as their squares. Which method below correctly does this?
hard
A. def squares(n): result = {} for i in range(1, n+1): result[i] = i * i return result
B. def squares(n): result = [] for i in range(n): result.append(i * i) return result
C. def squares(n): return {i: i + i for i in range(1, n)}
D. def squares(n): for i in range(1, n+1): print(i * i)

Solution

  1. Step 1: Understand the requirement

    The method must return a dictionary with keys 1 to n and values as squares.
  2. Step 2: Check each option

    The loop building result = {}, setting result[i] = i * i for i in range(1, n+1), and returning result is correct. Returning a list fails. The comprehension {i: i + i for i in range(1, n)} uses doubles instead of squares and misses key n. Printing without returning fails.
  3. Final Answer:

    def squares(n): result = {} for i in range(1, n+1): result[i] = i * i return result -> Option A
  4. Quick Check:

    Return dict with squares = def squares(n): result = {} for i in range(1, n+1): result[i] = i * i return result [OK]
Hint: Return dict with keys and squares using loop [OK]
Common Mistakes:
  • Returning list instead of dict
  • Using wrong range limits
  • Printing instead of returning