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.
Jump into concepts and practice - no test required
def add(a, b): return a + b result = add(3, 4) print(result)
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Define method 'add' | Store function with parameters a, b | Method ready to call |
| 2 | Call add(3, 4) | Parameters a=3, b=4 | Enter method body |
| 3 | Calculate a + b | 3 + 4 | 7 |
| 4 | Return 7 | Return value sent back | Method call replaced by 7 |
| 5 | Assign result = 7 | Store 7 in variable 'result' | result = 7 |
| 6 | Print result | Output value of result | 7 |
| 7 | End | No more code | Program stops |
| Variable | Start | After Step 4 | After Step 5 | Final |
|---|---|---|---|---|
| a | N/A | 3 | 3 | 3 |
| b | N/A | 4 | 4 | 4 |
| result | Undefined | Undefined | 7 | 7 |
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 logicreturn statement do in Python?returnreturn statement sends a value back from the method to the caller.a and b?return to send back the sum a + b.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.def multiply(x, y):
return x * y
result = multiply(3, 4)
print(result)multiply returns the product of 3 and 4, which is 12.result stores 12, so print(result) outputs 12.def greet(name):
print("Hello, " + name)
message = greet("Alice")
print(message)message is None.print with return to send the greeting back.print to return inside the method. -> Option Dn and values as their squares. Which method below correctly does this?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.