Phase 1: Prepare
Step 1 Warm-up recall
From M5.L1: when does a function body run?
Private: nothing you type here is sent or stored.
Step 2 Learning goal
By the end of this lesson you can:
- write a function with parameters (Foundation);
- return a value and use it (Core);
- tell print from return (Core).
Phase 2: Teach
Step 3 A familiar example
A cashier enters a price and the till works out the price with tax. The price goes in, the amount to pay comes out. That is a function with a parameter and a return value.
Step 4 Clear explanation
A parameter is a name in the brackets of the def. An argument is the actual value you pass in the call. return sends a value back.
def with_tax(price, rate):
return price + price * rate
total = with_tax(200, 0.1)
print(total)
print(with_tax(50, 0.2))220.0
60.0return ends the function at once and hands the value to the caller. A function with no return gives back None.
| Code | What happens | Result |
|---|---|---|
| double(4) | returns 8 | 8 |
| print(double(4)) | shows the returned value | 8 |
| show(4) with only print inside | shows 8 but returns None | None |
| x = double(4) | stores the returned value | x is 8 |
Keep print and return apart. Print only shows text on the screen; return gives the value to the program so it can be stored, tested or used in another calculation. Arguments are matched to parameters in order, so with_tax(0.1, 200) would give a very different answer. Be careful with units: a function that expects metres must not be given feet.
Step 5 Worked example (4 marks)
For def area(w, h): return w * h find area(3, 4) + area(2, 5).
- area(3, 4) returns 12. (1 mark)
- area(2, 5) returns 10. (1 mark)
- Add the returned values: 12 + 10 = 22. (1 mark)
- Answer: 22. (1 mark)
Go deeper: the professor's notes
Says the job.
Parameters.
return.
Impact
| Lines of code | Fetches in a month |
|---|---|
| 11 | over 2.4 million |
At work: A shared helper
Your team copies a helper into five programs.
- Move it to one function.
- Test it once.
What a careful practitioner decides. A function others rely on needs a docstring and tests.
Phase 3: Practise
Step 6 Learner practice (instant-marked)
1. In def f(a, b), a and b are:
Names in the def.
2. In the call f(3, 4), 3 and 4 are:
Values passed in.
3. A function with no return statement gives back:
Python gives None.
4. To use a function result in later code, the function should:
Return hands over the value.
Phase 4: Close
Step 7 Recap
- Parameters are names; arguments are values.
- return hands a value back and ends the function.
- Print shows; return gives.
Step 8 Exit ticket (ungraded)
Write def half(n) that returns n divided by 2 and use it to print half(9).
Private: nothing you type here is sent or stored.
Step 9 Go further (Extension)
Write a function that converts kilograms to pounds and returns the result.
In industry: Ariane 5: four of seven variables protected
The inquiry found that only four of seven critical variables had been protected against overflow, partly to keep the computer within a workload target.
Link to this lesson. Decide on purpose which inputs a function checks, and write that down in its docstring.
Source: Wikipedia, Ariane flight V88, citing the Inquiry Board report
Common mistakes
| Mistake | Why it fails |
|---|---|
| Using print instead of return | The caller then gets None. |
| Arguments in the wrong order | They match parameters by position. |
| Mixing units | State the unit in the name or the comment. |
Limits. Functions can return several values and take optional parameters; this lesson keeps to the basics.