whatsafunction.sh

Intro to Programming

// Year 10s

Dr. Hughes

Instructions to make tea

1Step 1: boil water
2Step 2: get cup
3Step 3: get a teabag
4Step 4: put teabag in cup
5Step 5: add boiled water to cup
6Step 6: wait 1 minute
7Step 7: use spoon to remove teabag

// seven steps. TeaPT is going to follow these, in order, every time.

How to make four cups of tea

Cup 1

1Step 1: boil water
2Step 2: get cup
3Step 3: get a teabag
4Step 4: put teabag in cup
5Step 5: add boiled water to cup
6Step 6: wait 1 minute
7Step 7: use spoon to remove teabag
·

Cup 2

1Step 1: boil water
2Step 2: get cup
3Step 3: get a teabag
4Step 4: put teabag in cup
5Step 5: add boiled water to cup
6Step 6: wait 1 minute
7Step 7: use spoon to remove teabag
·

Cup 3

1Step 1: boil water
2Step 2: get cup
3Step 3: get a teabag
4Step 4: put teabag in cup
5Step 5: add boiled water to cup
6Step 6: wait 1 minute
7Step 7: use spoon to remove teabag
·

Cup 4

1Step 1: boil water
2Step 2: get cup
3Step 3: get a teabag
4Step 4: put teabag in cup
5Step 5: add boiled water to cup
6Step 6: wait 1 minute
7Step 7: use spoon to remove teabag

// 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.

Why can’t Ctrl+C, Ctrl+V do all the work?

1Step 1: boil water
2Step 2: get cup
3Step 3: get a teabag
4Step 4: put teabag in cup
5Step 5: add boiled water to cup
6Step 6: wait 1 minute
7Step 7: use spoon to remove teabagcopy 1 — pasted
8Step 8: wait 1 minute
9Step 9: use spoon to remove teabagcopy 2 — pasted
10Step 10: wait 1 minute
11Step 11: use spoon to remove teabagcopy 3 — pasted
12Step 12: wait 1 minute
13Step 13: use spoon to remove teabagcopy 4 — pasted
14Step 14: wait 1 minute
15Step 15: use spoon to remove teabagcopy 5 — pasted

// the teabag gets stewed: each duplicated block runs the whole recipe again.

// copy-paste struck again: the same 2 lines, pasted 5 times over.

It’s fine, we’ll just be careful

Customer 1

"Tea, please"

Customer 2

"Tea with milk, please"

Customer 3

"Tea with oat milk, please"

Customer 4

"Coffee, please"

1Step 1: boil water
2Step 2: get cup
3Step 3: get a teabag
4Step 4: put teabag in cup
5Step 5: add boiled water
6Step 6: wait 1 minute
7Step 7: remove teabag
1Step 1: boil water
2Step 2: get cup
3Step 3: get a teabag
4Step 4: put teabag in cup
5Step 5: add boiled water
6Step 6: wait 1 minute
7Step 7: remove teabag
8Step 8: add milk
1Step 1: boil water
2Step 2: get cup
3Step 3: get a teabag
4Step 4: put teabag in cup
5Step 5: add boiled water
6Step 6: wait 1 minute
7Step 7: remove teabag
8Step 8: add oat milk
1Step 1: boil water
2Step 2: get cup
3Step 3: get a coffee bag
4Step 4: put bag in cup
5Step 5: add boiled water
6Step 6: wait 30s
7Step 7: remove bag
8Step 8: add milk

Now we’re managing 4 things ⚠️

C1 — tea

1Step 1: boil water
2Step 2: get cup
3Step 3: get a teabag
4Step 4: put teabag in cup
5Step 5: add boiled water
6Step 6: wait 1 minute
7Step 7: remove teabag

C2 — tea + milk

1Step 1: boil water
2Step 2: get cup
3Step 3: get a teabag
4Step 4: put teabag in cup
5Step 5: add boiled water
6Step 6: wait 1 minute
7Step 7: remove teabag
8Step 8: add milk

C3 — oat milk

1Step 1: boil water
2Step 2: get cup
3Step 3: get a teabag
4Step 4: put teabag in cup
5Step 5: add boiled water
6Step 6: wait 1 minute
7Step 7: remove teabag
8Step 8: add oat milk

C4 — coffee

1Step 1: boil water
2Step 2: get cup
3Step 3: get a coffee bag
4Step 4: put bag in cup
5Step 5: add boiled water
6Step 6: wait 30s
7Step 7: remove bag
8Step 8: add milk

// 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.

If only…

If only there was a better way…

We wrap the steps in a function:

make_tea(with_milk)
1Step 1: boil water
2Step 2: get cup
3Step 3: get a teabag
4Step 4: put teabag in cup
5Step 5: add boiled water to cup
6Step 6: wait 1 minute
7Step 7: use spoon to remove teabag
 if with_milk:
8Step 8: add milk

// one name. One recipe. Call it with or without milk.

More functions for your functions

make_hotdrink(with_milk, ???)

// What could go in that blank? Let's build it up one line at a time.

1Step 1: boil water # same for tea, coffee, hot chocolate…
2Step 2: get cup # still the same, whatever's going in the cup
3… ? ? ? … # teabag? coffee scoop? hot chocolate powder?
4… ? ? ? … # whatever it is, it still goes "in the cup"
5… ? ? ? …

// the first two steps are the same for any hot drink. The rest depend on which one.

More functions for your functions

make_hotdrink(with_milk, drink)
1Step 1: boil water
2Step 2: get cup
3if drink == "tea":
  Step 3: get a teabag
4elif drink == "coffee":
  Step 3: get a spoonful of coffee
5Step 4: put drink in cup
6Step 5: add boiled water to cup
7Step 6: wait 1 minute
8Step 7: use spoon to remove bag
 if with_milk:
9Step 8: add milk

// one recipe, many drinks — and with_milk still works exactly like before.

But… what is a function?

It's the same idea, everywhere

// in maths — in code

y = m·x + b
1def f(x):
2return m * x + b

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.

Code examples with functions

0 19 0 x y = 2x + 1

Plot y = 2x + 1 — one call at a time. Click through:

1plot(0, f(0)) # (0, 1)
2plot(1, f(1)) # (1, 3)
3plot(2, f(2)) # (2, 5)
4plot(3, f(3)) # (3, 7)
5plot(4, f(4)) # (4, 9)
6plot(5, f(5)) # (5, 11)
7plot(6, f(6)) # (6, 13)
8plot(7, f(7)) # (7, 15)
9plot(8, f(8)) # (8, 17)
10plot(9, f(9)) # (9, 19)

// 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.

Code examples with functions

0 19 0 x y = 2x + 1

For our line, m = 2 and b = 1:

1m = 2
2b = 1
  
3for x in range(10):
4plot(x, f(x))

// 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.

Code examples with functions

0 19 0 x y = 2x + 1

f(x) only ever draws this line. Let's write one that draws any line:

1def plot_straight_line(m, b):
2for x in range(10):
3plot(x, m * x + b)
  
4plot_straight_line(2, 1)

// same ten points, same line — but plot_straight_line works for any m and b you hand it, not just this one.