whatsafunction.sh
// Year 10s
Dr. Hughes
// seven steps. TeaPT is going to follow these, in order, every time.
Cup 1
Cup 2
Cup 3
Cup 4
// 7 lines for one cup. Add a second cup and…
// 14 lines. Copy, paste, repeat…
// 21 lines — and it’s the same recipe every time.
// 28 lines. Four cups, one recipe, four copies.
Spot the problem?
// the teabag gets stewed: each duplicated block runs the whole recipe again.
// copy-paste struck again: the same 2 lines, pasted 5 times over.
Customer 1
"Tea, please"
Customer 2
"Tea with milk, please"
Customer 3
"Tea with oat milk, please"
Customer 4
"Coffee, please"
C1 — tea
C2 — tea + milk
C3 — oat milk
C4 — coffee
// one recipe, one customer. Easy.
// new customer, new step. Append it to the recipe.
// a tweak. Just edit step 8 — what could go wrong?
// a brand-new recipe. Edit step 3, edit step 6, hope nothing else breaks.
We wrap the steps in a function:
// one name. One recipe. Call it with or without milk.
// What could go in that blank? Let's build it up one line at a time.
// the first two steps are the same for any hot drink. The rest depend on which one.
// one recipe, many drinks — and with_milk still works exactly like before.
// in maths — in code
Ultimately: a function is just a series of repeatable instructions — sometimes based on input.
// same shape, whether you write it with symbols or with code.
Plot y = 2x + 1 — one call at a time. Click through:
// same f(x) from before, call after call — only the x changes.
// ten points, one straight line. And "f(x)" got typed out ten times to get there.
For our line, m = 2 and b = 1:
// watch that: the loop just drew all ten points, instantly — no clicking required.
// for x in range(10): repeats the line below it 10 times, once for each value of x from 0 to 9. More on loops next time.
f(x) only ever draws this line. Let's write one that draws any line:
// same ten points, same line — but plot_straight_line works for any m and b you hand it, not just this one.