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

Sub-routines and a project

Week 5 · Unit 7.1: Block it out · 3 lessons
1

Mini-programs inside a program

Computing 7: Unit 7.1, pp. 8–43
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.

Startrun: bannerINPUT nameOUTPUT "Hello", namerun: bannerEnd
Worked examples
StartOUTPUT "*****"OUTPUT "QUIZ TIME"OUTPUT "*****"End
This is the flowchart of the sub-routine called banner. It has its own start and stop. It shows three lines.
OrderWhat runsOutput
1banner*****, QUIZ TIME, *****
2INPUT name(Ravi is typed)
3OUTPUTHello Ravi
4banner*****, 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.
Sub-routinedefinebannersayQUIZ TIMEfor1secondsMain scriptwhenclickedbanneraskName?and waitbanner
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?
Startrun: cheerOUTPUT "Goal!"run: cheerrun: cheerEnd
2
The block wave makes a sprite say Hi! for 1 second. What does the sprite say, and how many times?
Sub-routinedefinewavesayHi!for1secondsMain scriptwhenclickedwavewavewavesayBye!for1seconds
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.
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?
StartINPUT namerun: greetOUTPUT "See you soon"End