Phase 1: Prepare
Step 1 Warm-up recall
From M6.L3: which structure finds a value by name?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- combine functions into a design (Foundation);
- test a function on its own (Core);
- trace the whole design (Core).
Phase 2: Teach
Step 3 A familiar example
A builder checks each beam before the roof goes on. A programmer tests each function before the whole is built, because a fault is far easier to find in a small part.
Step 4 Clear explanation
A modular design builds a solution from separate functions with clear interfaces: the interface is what a function needs and what it gives back, and the inside can change without breaking the rest. Here are three invented functions for the receipt task.
| Function | Input | Output |
|---|---|---|
| total_of(prices) | Prices 12, 8, 20 | 40 |
| tax_on(amount, rate) | Amount 40, rate 10 percent (invented) | 4 |
| grand_total(prices, rate) | Prices 12, 8, 20 and rate 10 percent | 44 |
The third function calls the first two: grand_total adds up the prices (40), finds the tax (10 percent of 40 is 4) and adds them to give 44. To test, check each function alone against a case you worked out by hand, including a boundary case such as an empty list of prices. Then test the whole. If grand_total fails but total_of and tax_on pass, the fault is in how they are combined. This is far quicker than hunting through everything at once. The same practice is used in every programming language.
Step 5 Worked example (4 marks)
Prices are 10, 15 and 25 and the invented tax rate is 10 percent. Find grand_total.
- total_of: 10 + 15 + 25 = 50. (1 mark)
- tax_on: 10 percent of 50 is 5. (1 mark)
- Add: 50 + 5 = 55. (1 mark)
- Answer: 55. (1 mark)
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. An interface says:
It is the visible contract.
2. Where should testing begin?
Small parts expose faults quickly.
3. If total_of and tax_on pass but grand_total fails, the fault is probably in:
Both parts work alone.
4. With prices 20 and 30 and invented tax of 10 percent, the grand total is:
50 plus 5 tax.
Phase 4: Close
Step 7 Recap
- Build from functions with clear interfaces.
- Test each function alone against hand-worked cases.
- Then test the combination.
Step 8 Exit ticket (ungraded)
Work out grand_total for prices 5 and 15 with 10 percent tax.
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Add an empty-list boundary case to the tests and say what total_of should return.
Common mistakes
| Mistake | Why it fails |
|---|---|
| Testing only the whole program | Test the parts first. |
| Changing a function interface without telling others | Everything that calls it may break. |
| Skipping edge cases such as empty input | Test the boundaries from Module 3. |
Limits. The tax rate and prices are invented; real tax rules differ and must be taken from the authority concerned.