Phase 1: Prepare
Step 1 Warm-up recall
From Module 4: how do you read the last item of a list?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- define and call a function (Foundation);
- explain why reuse helps (Core);
- trace the order in which lines run (Core).
Phase 2: Teach
Step 3 A familiar example
A shop prints the same receipt footer on every sale. Typing it out each time invites mistakes, so it is written once and used again whenever it is needed.
Step 4 Clear explanation
A function is a named block of code. def defines it; writing its name with brackets calls it. The body is indented.
def greet():
print("Welcome")
print("Thank you")
greet()
greet()Welcome
Thank you
Welcome
Thank youDefining a function does not run it. Python first reads the def, remembers the name, then runs the later lines. Each call jumps into the body and then returns to the line after the call. A function must be defined before the line that calls it runs.
The big gain is reuse. If the footer changes, you edit one function, and every call shows the new text. Copied code must be corrected in every copy, and one copy is usually missed.
Step 5 Worked example (4 marks)
Trace def hi(): print("A") followed by hi(), print("B"), hi().
- The def line only stores the function; nothing prints. (1 mark)
- The first call prints A. (1 mark)
- Then B is printed, then the second call prints A. (1 mark)
- Output: A, B, A. (1 mark)
Go deeper: the professor's notes
Defines.
name().
Indented.
Two ways
| Style | Effect |
|---|---|
| copy the code | fix every copy |
| one function | fix once |
At work: Footer text
A footer must change on every receipt.
- Put it in a function.
- Edit the one function.
What a careful practitioner decides. Use a function for any text or rule that repeats.
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. The keyword that defines a function is:
def starts a function.
2. A function body runs when:
Defining does not run it.
3. To call a function named show, write:
Name and brackets.
4. The main gain of a function is:
Reuse.
Phase 4: Close
Step 7 Recap
- def defines; name() calls.
- The body is indented and runs only on a call.
- One function replaces many copies.
Step 8 Exit ticket (ungraded)
Write a function line() that prints ten dashes, then call it twice.
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Write a function that prints a three-line address block and call it for two customers.
In industry: Ariane 5: code reused without a check
The first Ariane 5 flight, on 4 June 1996, ended about 40 seconds after lift-off. Software reused from Ariane 4 converted a 64-bit floating-point number to a 16-bit signed integer; the value was larger than that integer could hold, and the conversion failed.
Link to this lesson. A function should check that its input fits what it was written for, especially when it is reused in a new setting.
Source: Report by the Inquiry Board, Ariane 5 Flight 501 Failure (19 July 1996)
Common mistakes
| Mistake | Why it fails |
|---|---|
| Defining but never calling | Nothing runs until the call. |
| Forgetting the brackets in a call | show() calls; show only names it. |
| Calling before the def has run | Define first, call after. |
Limits. This lesson uses functions with no inputs; inputs come next.