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

Repeat and reuse

Week 5 · 1.3 Creating algorithms · 3 lessons
3

Call a sub-routine

Computing 9: Topic 1.3, pp. 46–79
GoalDeclare and call sub-routines in flowcharts and pseudocode, and follow the order in which a program runs when it calls one.
New words
sub-routine · a small algorithm with its own name, which other algorithms can use again and againcall · to run a sub-routine by writing its name followed by bracketspredefined sub-routine · a ready-made sub-routine, for example from a library, that you only need to call
Explanation

A sub-routine is a small algorithm with a name of its own. You write it once, and then you call it by writing its name and brackets, such as water(). At a call the main algorithm pauses. The sub-routine runs from its first line to its last, and then the main algorithm carries on with the line after the call. Words with a fixed meaning in pseudocode, such as IF, FOR and SUB-ROUTINE, are key words, so they cannot be used as names for your own variables or sub-routines. One sub-routine can be called many times, even inside a loop or an IF, so you never have to write its lines again. Pseudocode has no fixed way to declare one: SUB-ROUTINE name() ends with END SUB, PROCEDURE name() ends with ENDPROCEDURE, and DEF name() has no end word because the indentation shows where it stops. A predefined sub-routine is called in just the same way, after you import the library it lives in.

Pseudocode
1SUB-ROUTINE water()
2    OUTPUT "Watering"
3    moisture ← moisture + 40
4END SUB
5moisture ← 20
6FOR day ← 1 TO 3
7    IF moisture < 50 THEN
8        water()
9    ENDIF
10    moisture ← moisture - 15
11NEXT day
12OUTPUT "Moisture: ", moisture
Output
Watering
Watering
Moisture: 55
Worked examples
Pseudocode
1SUB-ROUTINE banner()
2    OUTPUT "*****"
3END SUB
4banner()
5OUTPUT "Welcome to the club"
6banner()
Output
*****
Welcome to the club
*****
The order of running. The sub-routine banner() only outputs a line of stars. The main part has three steps. First it calls banner(), which outputs the stars. Then it outputs the welcome. Then it calls banner() a second time. The sub-routine is written once but runs twice.
StyleFirst lineLast line
Pseudocode, version 1SUB-ROUTINE banner()END SUB
Pseudocode, version 2PROCEDURE banner()ENDPROCEDURE
Python styleDEF banner():none: the indent ends it
Python (real)def banner():none: the indent ends it
Different ways to write the same sub-routine. The last row is real Python; the others are pseudocode styles. The call banner() is written in the same way in all of them.
Startmoisture ← 20day ← 1day <= 3?yesmoisture < 50?yesnocall water()moisture ← moisture - 15day ← day + 1noOUTPUT "Moisture: ", moistureEnd
The plant program from the top of the lesson as a flowchart. Its sub-routine water() would be a second, separate flowchart (not drawn here). The real flowchart symbol for a call is a box with double side lines; here it is drawn as a box that starts with call. Trace: moisture 20 is below 50, so water() runs (60), then 15 is taken away (45). Day 2: 45 is below 50, so water() runs (85) and then 70. Day 3: 70 is not below 50, so there is no call, and 15 less makes 55.
Class activity

Helper cards (no computer). On a small card write a sub-routine, for example SUB-ROUTINE clap() with the line ‘clap twice’ and END SUB. On a big sheet write a main program of six lines that includes three calls to clap(). A partner reads the main program aloud with a finger on the current line. At every call the finger jumps to the card, does the actions, and jumps back to the line after the call. Then write a sub-routine of your own, such as stamp() or hop(), and add a FOR loop to the main sheet that calls it four times.

Practice
1
Write down everything that this algorithm outputs.
Pseudocode
1SUB-ROUTINE cheer()
2    OUTPUT "Hooray!"
3END SUB
4FOR round ← 1 TO 3
5    OUTPUT "Round ", round
6    IF round = 2 THEN
7        cheer()
8    ENDIF
9NEXT round
2
Complete a trace table with the columns n and level, and give the output.
Pseudocode
1SUB-ROUTINE sip()
2    level ← level - 25
3END SUB
4level ← 100
5FOR n ← 1 TO 3
6    sip()
7    OUTPUT level
8NEXT n
3
Write a sub-routine hello() that outputs Welcome, and a main part that uses a loop to call it 4 times. How many lines are output?
4
This program should output Ding twice, but it outputs nothing. Explain why and correct it.
Pseudocode
1SUB-ROUTINE ding()
2    OUTPUT "Ding"
3END SUB
4FOR bell ← 1 TO 2
5    ding
6NEXT bell