Week 14 · Unit 6: Sub-routines in programming · 3 lessons
1
One job, many times
Computing 6: Unit 6, pp. 78–91
GoalSpot steps that repeat in an algorithm and turn them into a sub-routine that is called several times.
New words
sub-routine · a named set of instructions that does one job and can be used again and againcall · to use a sub-routine by writing its name where its job is neededprocedure · the name that programmers often use for a sub-routine in program codealgorithm · a list of steps, in the right order, that solves a problem
Explanation
Many jobs contain the same steps again and again. Instead of writing the steps each time, we give them a name and write them once. A named set of steps that does one job is a sub-routine. The main algorithm says ‘do the sub-routine’ wherever the job is needed. This is called calling the sub-routine. Sub-routines make an algorithm shorter and easier to read. They also make it easier to correct, because a mistake only has to be fixed in one place. In program code a sub-routine is often called a procedure. In a flowchart a call is drawn as one box with the name of the sub-routine in it.
Main algorithm: plant a row
Sub-routine: plant one seed
1 Plant one seed
1 Dig a small hole
2 Move one step forward
2 Drop in one seed
3 Plant one seed
3 Cover the seed with soil
4 Move one step forward
4 Stop
5 Plant one seed
6 Stop
Worked examples
Line
Main algorithm: plant 10 seeds
1
Repeat 9 times: plant one seed, then move one step forward
2
Plant one seed
3
Stop
To plant a row of 3 seeds the long way, Aisha would write 3 steps for each seed and 2 moves: 11 steps. With the sub-routine the main algorithm has only 6 lines, and the sub-routine is written once. For a row of 10 seeds the long way needs 39 steps, but the main algorithm is still short if it uses a loop.
Later Aisha decides to drop two seeds into each hole. With a sub-routine she changes one line, line 2 of ‘plant one seed’, and every seed in the row is planted the new way. Without a sub-routine she would have to find and change every single copy.
paint with red→clean the brush→paint with blue→clean the brush→paint with yellow→clean the brush
Leo is painting with three colours. After each colour he cleans his brush by dipping it in water, wiping it on a cloth and checking that it is clean. These steps are the same each time, so they can be one sub-routine called ‘clean the brush’.
Class activity
Human sub-routine (no computer): invent a three-move dance, such as clap, stamp and spin, and name it ‘Wiggle’. An adult reads this algorithm: Wiggle, step forward, Wiggle, step forward, Wiggle. You do it. Then write the same algorithm without the sub-routine and count the lines. Which version is shorter? Which is easier to change if you want Wiggle to have four moves instead of three? Now invent a new sub-routine of your own and a main algorithm that calls it three times.
Practice
1
Write the main algorithm for painting four fence posts, using a sub-routine called ‘paint one post’. Then write the steps of the sub-routine.
Main: 1 Start. 2 Paint one post. 3 Move to the next post. 4 Paint one post. 5 Move to the next post. 6 Paint one post. 7 Move to the next post. 8 Paint one post. 9 Stop. Sub-routine ‘paint one post’: 1 Dip the brush. 2 Paint the post from top to bottom. 3 Check that no gaps are left. (A loop is also correct for the main algorithm.)
2
Find the steps that repeat. Rewrite the algorithm with a sub-routine called ‘stamp one card’.
Step
Instruction
1
Pick up the stamp
2
Press it on the ink
3
Press it on the card
4
Put the stamp down
5
Move to the next card
6
Pick up the stamp
7
Press it on the ink
8
Press it on the card
9
Put the stamp down
10
Move to the next card
11
Pick up the stamp
12
Press it on the ink
13
Press it on the card
14
Put the stamp down
Steps 1 to 4 repeat for every card. Sub-routine ‘stamp one card’: 1 Pick up the stamp. 2 Press it on the ink. 3 Press it on the card. 4 Put the stamp down. Main: stamp one card, move to the next card, stamp one card, move to the next card, stamp one card.
3
The flowchart calls ‘pour one glass’, which has 3 steps. How many times is it called? How many steps are carried out altogether, not counting start and stop?
It is called 3 times. 3 × 3 = 9 steps inside the calls, and 2 moves, so 11 steps altogether.
4
Why is it easier to correct a mistake when the program uses a sub-routine?
The steps are written in only one place. When the mistake is fixed there, every call uses the corrected steps. Without a sub-routine every copy has to be found and fixed.