Phase 1: Prepare
Step 1 Warm-up recall
From M6.L1: name two parts of the receipt task.
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- define a function, input and output (Foundation);
- trace a call by hand (Core);
- write a short function from a description (Core).
Phase 2: Teach
Step 3 A familiar example
A calculator key such as the square root takes a number, gives back a number and hides the working. A function in a program does the same job for any step you name.
Step 4 Clear explanation
A function is a named part that takes inputs (also called arguments or parameters), does its work and gives back an output (the returned value). Using a function is a call. Here is an invented function: average(a, b) returns (a + b) ÷ 2.
| Call | Working | Result |
|---|---|---|
| average(4, 10) | (4 + 10) ÷ 2 | 7 |
| average(3, 8) | (3 + 8) ÷ 2 | 5.5 |
| average(6, 6) | (6 + 6) ÷ 2 | 6 |
To trace a call, replace the inputs by their values, do the working in order and write down the output. A function can be called many times with different inputs, so the working is written once and reused. A function should depend only on its inputs, so the same inputs always give the same output, which is what makes it easy to test. In a programming language, the rules for naming and calling functions differ in detail, but the idea is the same.
Step 5 Worked example (4 marks)
Trace average(6, 11) using average(a, b) = (a + b) ÷ 2.
- Replace the inputs: (6 + 11) ÷ 2. (1 mark)
- Add: 17 ÷ 2. (1 mark)
- Divide: 8.5. (1 mark)
- Answer: 8.5. (1 mark)
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. A function gives back:
The output is the returned value.
2. average(8, 12) = (a + b) ÷ 2 equals:
(8 + 12) ÷ 2 = 10.
3. The inputs to a function are also called:
They are the values passed in.
4. Calling a function twice with the same inputs should give:
The output depends only on the inputs.
Phase 4: Close
Step 7 Recap
- A function has inputs, working and an output.
- Trace a call by replacing inputs with values.
- The same inputs should give the same output.
Step 8 Exit ticket (ungraded)
Trace average(5, 9) and average(0, 7).
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Write a function double(x) that returns x × 2 and trace it for 6 and 0.
Common mistakes
| Mistake | Why it fails |
|---|---|
| Mixing up the order of inputs | Match each input to its name in order. |
| Forgetting the output | A function must give something back. |
| Hiding extra inputs inside | List every input the function needs. |
Limits. The functions here are written in plain notation, not in a particular programming language.