AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Reuse with sub-routines

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 rowSub-routine: plant one seed
1 Plant one seed1 Dig a small hole
2 Move one step forward2 Drop in one seed
3 Plant one seed3 Cover the seed with soil
4 Move one step forward4 Stop
5 Plant one seed
6 Stop
Worked examples
LineMain algorithm: plant 10 seeds
1Repeat 9 times: plant one seed, then move one step forward
2Plant one seed
3Stop
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.
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.
2
Find the steps that repeat. Rewrite the algorithm with a sub-routine called ‘stamp one card’.
StepInstruction
1Pick up the stamp
2Press it on the ink
3Press it on the card
4Put the stamp down
5Move to the next card
6Pick up the stamp
7Press it on the ink
8Press it on the card
9Put the stamp down
10Move to the next card
11Pick up the stamp
12Press it on the ink
13Press it on the card
14Put the stamp down
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?
Startcall: pour one glassmove to the next seatcall: pour one glassmove to the next seatcall: pour one glassEnd
4
Why is it easier to correct a mistake when the program uses a sub-routine?