Compose two small functions
Quick review (2 minutes)
A stall at a market works out a bill in three small jobs.
Python works out every argument of a call before the call begins. When an argument is itself a call, that call finishes first and its return value becomes the argument of the call that receives it. For the receiving call to get a value other than None, the inner function has to return one.
Write the task as a list of small jobs, each one taking some values and giving one value back.
Write one function for each job, with a parameter for each value it takes. Return its result.
Test each function that calls no other function alone, with a few arguments, including any boundary value. Compare with results worked out by hand.
Join the functions by passing one call as the argument of another, or by giving a result a name and passing the name. Then test the joined function in the same way.
Trace a joined line by working out each argument before the call that receives it, and replace each finished call with the value it returns.
A function that prints where it should return breaks the chain. If subtotal displayed the product and had no return line, with_fee would receive None. The comparison None < 20 would then stop the program with a TypeError, because Python provides no order comparison between None and a number. Return from every function whose value another function needs.
The other slip is one long function that does every job. Split it where one value is finished and the next job begins, so that each job can be called and tested by its own name.
1 In plain words
A stall at a market works out a bill in three small jobs. One person multiplies the price by the number of items. A second adds a bag fee to a small order. A third writes the line on the receipt. Each person can be checked alone, and the bill passes from one to the next.
A program can be split the same way. Breaking a task into small functions, each with one job, is called decomposition. Using one function's return value as another function's argument is called composition. Those two words are this lesson's own shorthand, and the rule that makes composition work is Python's: every argument is worked out before the call it belongs to begins.
The stall's rule is invented for this lesson, and the receipt program below follows it. The bill is the price times the count. A bill under 20 coins gets a bag fee of 3 coins, and a bill of 20 or more gets none. The function with_fee returns from inside an if block for the small bill and from the line after it for the rest.
Remember one sentence: a call used as an argument finishes first, and its value becomes the argument. To look it up, see section 6.3.4 of the Python 3.13 language reference, on calls.
2 Picture it
A table of three calls and what each gives back. subtotal(4, 3) gives back 12. with_fee(12) gives back 15. receipt(4, 3) gives back the text Total: 15 coins. The row for receipt is marked.
3 Why
In with_fee(subtotal(price, count)), the argument of with_fee is itself a call. Python works out an argument before it makes the call that uses it, so subtotal runs first and its return value is passed to with_fee.
Composition needs return values. A function that prints its result and has no return line gives back None, and the next function would receive None in place of a number.
Small functions can be tested one at a time. By the stall's rule above, with_fee has a boundary at 20: its if takes one branch for 19 and the other for 20. Those two values test it without any price or count. Test each part alone first. If the receipt is still wrong, look next at the way the parts are joined, and at inputs that no test tried.
4 The rule
Python works out every argument of a call before the call begins.
When an argument is itself a call, that call finishes first and its return value becomes the argument of the call that receives it. For the receiving call to get a value other than None, the inner function has to return one.
5 How
- Write the task as a list of small jobs, each one taking some values and giving one value back.
- Write one function for each job, with a parameter for each value it takes. Return its result.
- Test each function that calls no other function alone, with a few arguments, including any boundary value. Compare with results worked out by hand.
- Join the functions by passing one call as the argument of another, or by giving a result a name and passing the name. Then test the joined function in the same way.
- Trace a joined line by working out each argument before the call that receives it, and replace each finished call with the value it returns.
6 Worked example
Join three functions into a receipt
def subtotal(price, count):
return price * count
def with_fee(amount):
if amount < 20:
return amount + 3
return amount
def receipt(price, count):
total = with_fee(subtotal(price, count))
return f"Total: {total} coins"
print(receipt(4, 3))
print(receipt(6, 5))
Standard input
Empty
Standard output
Total: 15 coins
Total: 30 coins
Predict the two output lines first. Then save the code as receipt.py and run the file, or follow the trace below. In each call of receipt, subtotal runs before with_fee.
Test one function alone at its boundary
def with_fee(amount):
if amount < 20:
return amount + 3
return amount
print(with_fee(19))
print(with_fee(20))
Standard input
Empty
Standard output
22
20
Predict the two output lines first. Then save the code as fee_test.py and run the file. 19 is under 20, so the fee is added. 20 is not under 20, so the indented return line is skipped and the amount comes back unchanged.
Trace
| Call | Arguments | Gives back | Explanation |
|---|---|---|---|
receipt(4, 3) starts | price 4, count 3 | — | The body of receipt begins. Its first line holds two more calls. |
subtotal(price, count) | 4 and 3 | 12 | This call is an argument of with_fee, so it finishes first: 4 * 3 is 12. |
with_fee(12) | 12 | 15 | By the stall's rule, 12 is under 20, so the fee of 3 is added and 15 comes back. total now names 15. |
receipt(4, 3) ends | — | Total: 15 coins | The return line builds the text and gives it back to the print call, which displays it. |
7 The trap
A function that prints where it should return breaks the chain. If subtotal displayed the product and had no return line, with_fee would receive None. The comparison None < 20 would then stop the program with a TypeError, because Python provides no order comparison between None and a number. Return from every function whose value another function needs.
The other slip is one long function that does every job. Split it where one value is finished and the next job begins, so that each job can be called and tested by its own name.
Quick checks
Quick check 1 of 2
Using the receipt program from the worked example, what does print(receipt(5, 4)) display? Name the value each of the two inner calls gives back.
Show the steps and the answer
It displays Total: 20 coins. subtotal(5, 4) gives back 20, and with_fee(20) gives back 20.
Why: 5 * 4 is 20. 20 is not under 20, so no fee is added, and the receipt line is built from 20.
Source: Python Software Foundation, Python 3.13 Language Reference, 6.3.4 Calls
Quick check 2 of 2
Predict the two lines this program displays. The program reads no input.
def triple(n):
return n * 3
def add_ten(n):
return n + 10
print(add_ten(triple(4)))
print(triple(add_ten(4)))Show the steps and the answer
It displays 22 and then 42.
Why: In the first line the call triple(4) is an argument, so it finishes first and gives back 12, and add_ten(12) gives back 22. In the second line add_ten(4) gives back 14, and triple(14) gives back 42.
Source: Python Software Foundation, Python 3.13 Language Reference, 6.3.4 Calls
Sources
- Python 3.13 Language Reference, 6.3.4 Calls. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Language Reference, 7.6 The return statement. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Language Reference, 8.1 The if statement. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Tutorial, 4.8 Defining Functions. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Language Reference, 6.10.1 Value comparisons. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.
- Python 3.13 Library Reference, Built-in Exceptions: TypeError. Python Software Foundation. Python Software Foundation License Version 2. Read 1 Dec 2026.