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
2

Choosing a sub-routine

Computing 7: Unit 7.1, pp. 8–43
GoalFollow, edit and correct a flowchart in which decisions choose which sub-routine to run.
New words
main program · the part of a program that decides what happens and calls the sub-routinesedit · to change a flowchart or program to add something or improve itunreachable · a path that can never be followed because of an earlier question
Explanation

In a longer program, the main flowchart is a kind of manager. It asks questions and calls the sub-routine that fits the answer. Each sub-routine does one job. To follow the program, trace the decisions and note which sub-routine is called, then trace that sub-routine. When you edit a program, you add one decision and one sub-routine and leave the others alone. When you correct a flowchart, check the order of the questions. An earlier question can swallow a case, and then a later path is unreachable. Test with a value on every boundary.

StartINPUT temptemp < 10?yesnorun: cold_advicetemp > 25?yesnorun: hot_advicerun: mild_adviceEnd
Worked examples
Sub-routineWhat it outputs
cold_adviceWear a coat, Drink something warm
hot_adviceWear a hat, Drink water
mild_adviceEnjoy the day
These are the sub-routines of the weather flowchart. The main flowchart decides which one runs. Each one is a short flowchart of its own.
temptemp < 10?temp > 25?Sub-routine that runs
4yes–cold_advice
30noyeshot_advice
18nonomild_advice
10nonomild_advice
25nonomild_advice
Trace the weather flowchart with five inputs. The boundary values are 10 and 25. Neither of them is below 10 or above 25.
Adding a new case is easy because the sub-routines are separate. For a ‘windy’ warning you would add one new decision and one new sub-routine, and the three old sub-routines would not change at all.
Class activity

Weather cards (no computer): write the three sub-routine names on three cards and put each card’s two outputs on its back. Write temperatures from 0 to 40 on small slips. Draw the weather flowchart on a big sheet. A partner picks a slip, you place a coin on the flowchart and answer each diamond, and then you pick up the matching card and read its back. Try 10, 25 and 26. Now invent a fourth card called ‘freezing_warning’ for temperatures below 0, and decide where its decision must go. If you have a computer or tablet, draw your new flowchart in a drawing program.

Practice
1
The sub-routine small_portion shows ‘Give 50 g’ and large_portion shows ‘Give 120 g’. Trace the flowchart for weights 3, 5 and 8.
StartINPUT weightweight < 5?yesnorun: small_portionrun: large_portionEnd
2
The flowchart should give hot advice for 30. It gives mild advice instead. Find the mistake and correct the flowchart.
StartINPUT temptemp > 10?yesnorun: mild_advicetemp > 25?yesnorun: hot_advicerun: cold_adviceEnd
3
Edit the weather flowchart so that a temperature above 35 runs a new sub-routine called heat_warning instead of hot_advice. Describe what you change.
4
Fill in the missing sub-routine for each temperature in the weather flowchart.
tempSub-routine that runs
9?
26?
15?