Math Bonk! Learning Centre Play the game

Coding with Nested Events and Subprograms

Dominoes falling

What your child is learning

A program that draws a square, then another square, then a third, quickly fills the screen with the same few blocks repeated. The Grade 7 answer is to write the square once, give that piece of code a name, and call on it whenever it is needed. That named piece is a subprogram. It works hand in hand with nested events, a repeated action inside another repeated action, which children met in Grade 6.

Children learn to:

This is the Coding Skills part of the Algebra strand in Ontario's curriculum. Loops, conditions and efficient code were introduced earlier; see the Grade 5 guide on coding with loops and conditions.

A subprogram (count the votes) is a named block that a loop can use again and again.A subprogram (count the votes) is a named block that a loop can use again and again.Startrepeat 4 [ count the votes · turn the page ]Stop
A subprogram (count the votes) is a named block that a loop can use again and again.

Why it matters

Every program of any size is built from named pieces that are written once and used many times. Learning to structure code that way is learning to manage complexity, and the same discipline pays off in mathematics: a formula is a subprogram for a calculation.

Nested loops turn multiplication into something visible, since a loop of 3 around a loop of 4 does something 12 times. And simulation is how statisticians and scientists explore chance when a formula is hard to write. The probability of two events can be investigated exactly this way.

How to teach it

1. Nest one count inside another

Read nested code from the inside out. In repeat 3 [repeat 4 [stamp]; turn], the inner loop stamps four times, then something is turned, and that whole routine happens three times: twelve stamps. Change where the semicolon sits and the result changes.

repeat 4 [ repeat 5 [ stamp, move 1 right ]; move 1 down, move 5 left ]
Inner loop: 5 stamps in a row.
Outer loop: does that 4 times, once per row.
Total: 4 × 5 = 20 stamps, a 4-by-5 array.

2. Name a piece of code

Define square (size): repeat 4 [move size, turn 90]. The main program can now say square (10), move 30, square (25). Change the definition from 90 to 60 and 4 to 6, and every call draws a hexagon instead. One edit, many effects: that is the point of a subprogram.

3. Put a condition inside a loop

A simulation is a loop with an "if" inside. repeat 100 [roll a die; if the roll is 6, add 1 to sixes]. Run it and compare the count with the expected 100 ÷ 6, about 17. A second event can be added: flip a coin and roll a die each time, and count only when both come out a chosen way.

4. Alter code and predict

Take a working program and ask what will happen before running the change. If repeat 4 [move 10, turn 90] becomes repeat 36 [move 10, turn 10], how many sides does the shape have, and how far does it travel? Predict, run, then explain any difference.

5. Judge efficiency

Two programs may draw the same picture. The better one is shorter, easier to check and easier to change. Count the blocks in each version and keep a tally of how many a subprogram saved.

Common mistakes

Do this together

Sticker robot. One of you is the programmer and the other is the robot. The robot needs grid paper and a sheet of small stickers. The programmer writes instructions on paper, using only: stamp, move 1 up, move 1 down, move 1 right, move 1 left, and repeat n [ ]. Start with a subprogram on its own line, for example define plus: stamp, move 1 right, stamp, move 1 left, move 1 up, stamp, move 1 down, move 1 left, stamp, move 1 right, move 1 down, stamp, move 1 up. (It stamps five stickers in a plus shape and brings the pen back to the centre.)

The robot follows the main program exactly as written, with no guessing. A good first challenge is repeat 2 [repeat 3 [plus; move 3 right]; move 3 down; move 9 left]. When the robot finishes, compare the page with what the programmer expected. Then swap roles and try to draw the same pattern with fewer instructions.

Printable worksheet

18 questions, a picture warm-up and drawing tasks, with an answer key on its own page. Print it or save it as a PDF.

Open the worksheet

Every worksheet is on the printable worksheets page.

Ontario curriculum: Grade 7, Algebra, expectation C3 · official document · U.S. Common Core crosswalk