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

Build and judge programs

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.

draw spokedefinedraw spokepen downmove40stepspen upmove-40stepsdraw wheeldefinedraw wheeldraw spoketurn ↻45degreesrepeat8Main scriptwhenclickederase allgo to x:-100y:0draw wheelgo to x:100y:0draw wheel
Worked examples
LevelBlockTimes it runs
main scriptdraw wheel2
draw wheeldraw spoke2 × 8 = 16
draw spokemove (40) steps16
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?
two popsdefinetwo popsstart soundPopwait0.2secondsstart soundPopwait0.2secondschantdefinechanttwo popsrepeat3start soundBoingMain scriptwhenclickedchantrepeat2
2
The sprite should draw a wheel with 8 spokes, but it draws an eight-sided shape instead. What is the bug?
draw spokedefinedraw spokepen downmove40stepspen updraw wheeldefinedraw wheeldraw spoketurn ↻45degreesrepeat8
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?
4
Write three questions to ask when a program with sub-routines does not work.