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

Reuse with sub-routines

Week 14 · Unit 6: Sub-routines in programming · 3 lessons
2

A sub-routine that draws

Computing 6: Unit 6, pp. 78–91
GoalWrite a drawing sub-routine for a pen, and use it several times from a main algorithm.
New words
pen down · a command that makes the sprite draw a line when it movespen up · a command that makes the sprite move without drawinghexagon · a flat shape with six straight sidesflowchart · a diagram of an algorithm made of shapes and arrows
Explanation

In Scratch a sprite can draw with a pen. When the pen is down, the sprite draws a line as it moves. When the pen is up, it moves without drawing. A sprite can draw a hexagon by repeating a move and a turn six times. Each turn is 60 degrees, because six equal turns make one full turn of 360 degrees and 360 ÷ 6 = 60. If a shape is needed many times, we write its steps once as a sub-routine. The main algorithm calls the sub-routine, and it moves the sprite with the pen up between the shapes so that no line joins them. A good drawing sub-routine ends with the sprite back where it started, facing the same way. Then the main algorithm is easy to follow.

x 240−240y 180−180start
Worked examples
Sub-routine: Draw HexagonInstruction
1Pen down
2Move 40 steps
3Turn right 60 degrees
4Repeat steps 2 and 3 five more times
5Pen up
6Stop
This is the sub-routine for the hexagon in the picture. Its sides are 40 steps long. The pen goes up at the end so that the next move does not draw. Six moves and six turns of 60 degrees bring the sprite back to the start.
Startgo to x: -120, y: 30call: Draw Hexagonmove 100 stepscall: Draw Hexagonmove 100 stepscall: Draw HexagonEnd
This main algorithm draws three hexagons in a row. The hexagon is 80 steps wide, so the sprite moves 100 steps between the starts. The pen is up for those moves, so the sprite draws only the hexagons.
Chen wants 12 hexagons in the pattern. The sub-routine does not change at all. The main algorithm only needs a repeat block that calls it 12 times, so one short sub-routine can make a very large drawing.
Class activity

Pencil sprite (no computer): on squared paper a pencil is the sprite. One ‘step’ is one square. Write this sub-routine, ‘Draw Flag’: pen down, move 4, turn right 90, move 2, turn right 90, move 4, turn right 90, move 2, turn right 90, pen up. Now write a main algorithm that calls Draw Flag, moves 6 squares with the pen up, and calls Draw Flag again. Swap algorithms with a friend and follow them with a pencil. Do you get two rectangles side by side? If you have a computer or tablet, build it in Scratch with the Pen extension.

Practice
1
Complete the sub-routine Draw Rectangle. The rectangle is 50 steps long and 20 steps high.
Sub-routine: Draw RectangleInstruction
1Pen down
2Move 50 steps
3Turn right ? degrees
4Move 20 steps
5Turn right ? degrees
6Repeat steps 2 to 5 ? more time
7Pen up
8Stop
2
Draw the flowchart for a main algorithm that calls Draw Rectangle, moves 80 steps with the pen up and calls Draw Rectangle again.
3
By mistake a sub-routine uses ‘turn right 90’ instead of ‘turn right 60’ and still repeats six times. The sides are 30 steps. What does the sprite draw?
4
Why should the sub-routine end with ‘pen up’? What would happen if it did not?