GoalExplain what a sub-routine is, why programmers use one, and follow a flowchart that calls it more than once.
New words
sub-routine · a named group of steps, kept apart from the main program, that runs whenever it is calledcall · to ask a sub-routine to runreuse · to use the same steps again, without writing them again
Explanation
A sub-routine is a named group of steps that is kept apart from the main program. You write it once and call it whenever you need it. The main program is shorter and easier to read, because a long job has a short name. If you want to change what the job does, you change it in one place. The real flowchart symbol for a call is a box with double lines down its sides. Our drawings show it as an ordinary box that starts with ‘run:’. The sub-routine has its own flowchart, with its own start and stop. When the sub-routine finishes, the main flowchart carries on from the next arrow.
Worked examples
This is the flowchart of the sub-routine called banner. It has its own start and stop. It shows three lines.
Order
What runs
Output
1
banner
*****, QUIZ TIME, *****
2
INPUT name
(Ravi is typed)
3
OUTPUT
Hello Ravi
4
banner
*****, QUIZ TIME, *****
Trace the main flowchart with the name Ravi. Each call of banner runs the three steps of the sub-routine and then returns to the main flowchart.
Scratch has the same idea. A block that you define is a sub-routine, and the stack under ‘define’ is its flowchart. The main script calls it by using the block twice.
Class activity
Recipe-card sub-routine (no computer): write a card called ‘make the dressing’ with three steps, for example ‘pour oil into a bowl’, ‘add lemon juice’ and ‘stir’. This card is your sub-routine. Now write two short main programs: ‘Salad A: wash the lettuce, make the dressing, serve’ and ‘Salad B: wash the tomatoes, make the dressing, serve’. A partner follows your main program with pretend ingredients. Each time they reach ‘make the dressing’, they take out your card and do its steps. Count how many steps they did in total. Then change the card to add a pinch of salt. How many main programs do you have to change? If you have a computer or tablet, build the same idea with a block in Scratch.
Practice
1
Trace the flowchart. The sub-routine cheer shows ‘Hip hip’ and then ‘Hooray!’. How many lines are shown, and what are they?
7 lines: Hip hip, Hooray!, Goal!, Hip hip, Hooray!, Hip hip, Hooray!. Cheer is called three times and shows 2 lines each time (6 lines), and the main flowchart shows 1 line (Goal!).
2
The block wave makes a sprite say Hi! for 1 second. What does the sprite say, and how many times?
The sprite says Hi! three times (one for each call of wave) and then Bye! once.
3
Find the steps that repeat, name a sub-routine for them, and write the main algorithm that uses it. 1 Put soil in the pot. 2 Press a seed in. 3 Add a little water. 4 Put soil in the pot. 5 Press a seed in. 6 Add a little water.
The steps 1–3 and 4–6 are the same. Sub-routine ‘plant a seed’: put soil in the pot, press a seed in, add a little water. The main algorithm: run plant a seed, run plant a seed. It has two lines instead of six.
4
The main flowchart has this call. What must you also draw so that the program is complete? Why does the main flowchart stay short?
You must draw the sub-routine’s own flowchart for greet, with its own start, steps and stop. The main flowchart stays short because the details of greeting are kept inside the sub-routine.