Week 15 · Unit 6: Sub-routines in programming · 3 lessons
2
Sub-routines inside sub-routines
Computing 6: Unit 6, pp. 78–91
GoalUse one sub-routine inside another, and find and fix bugs in programs that use sub-routines.
New words
bug · a mistake in a programdebug · to find and fix bugscall order · the order in which sub-routines are calledtest · to run a program or a part of a program to see if it works
Explanation
A sub-routine can call another sub-routine, in the same way as the main script does. This lets us build big jobs from small ones. To draw a wheel, one sub-routine draws a single spoke. A second sub-routine draws the wheel by calling the spoke sub-routine again and again, with a turn between the spokes. The main script only has to call draw wheel. When a program with sub-routines does not work, we debug it. First test each small sub-routine on its own. Then check the order of the calls. Then check the pen and the position and direction of the sprite after each call. A good sub-routine leaves the sprite where it found it, facing the same way.
Worked examples
Level
Block
Times it runs
main script
draw wheel
2
draw wheel
draw spoke
2 × 8 = 16
draw spoke
move (40) steps
16
Trace the program. The main script calls draw wheel 2 times. Each wheel calls draw spoke 8 times, so 16 spokes are drawn. Every spoke is followed by a turn of 45 degrees, and 8 × 45 = 360, so the sprite faces the same way again after each wheel.
Here is an order bug. Chen writes the main script as: draw wheel, go to x: 100 y: 0, draw wheel. He forgot the first go to, so the first wheel is drawn wherever the sprite was before, and not at x = -100. The program works, but the picture is wrong. Order matters, even when every block is correct.
Use this debugging list: 1 Click the small sub-routine on its own to see what it does. 2 Check the order of the calls. 3 Check that the pen goes up. 4 Check where the sprite is and which way it faces after each sub-routine. 5 Fix one thing at a time and test again.
Class activity
Greeting in layers (no computer): write three sub-routines on cards. ‘bow’ is step back, bend, step forward. ‘greet’ is smile, bow, wave. ‘welcome visitors’ calls greet three times, once for each visitor, and then says ‘Please come in’. A friend plays three visitors. Follow the cards: start with welcome visitors, then jump to greet, and from there jump to bow. Count how many times you bend. (It is 3.) Then swap one card for a wrong one on purpose and ask your friend to debug.
Practice
1
Trace the program. How many Pop sounds and how many Boing sounds are started?
The chant has 3 two-pops calls: 3 × 2 = 6 pops, and 1 boing. The main script calls chant 2 times: 12 pops and 2 boings.
2
The sprite should draw a wheel with 8 spokes, but it draws an eight-sided shape instead. What is the bug?
The draw spoke sub-routine does not move back to the centre. After each spoke the sprite stays at the end of the line, turns 45 degrees and draws the next line from there, so the 8 lines join up into an eight-sided shape. The fix is to add move (-40) steps after pen up.
3
The sub-routine ‘corner’ turns the sprite right 90 degrees and never turns back. The main script points the sprite in direction 90, calls corner 3 times and then moves 10 steps. Which way does the sprite move?
Direction 90 is right. Each corner turns it 90 degrees more: 180, 270 and then 360, which is the same as 0. Direction 0 is up, so the sprite moves 10 steps up.
4
Write three questions to ask when a program with sub-routines does not work.
For example: Does each sub-routine work on its own? Are the calls in the right order? Is the pen up or down when it should be? Is the sprite in the right place and facing the right way after each sub-routine?