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.
Worked examples
Sub-routine: Draw Hexagon
Instruction
1
Pen down
2
Move 40 steps
3
Turn right 60 degrees
4
Repeat steps 2 and 3 five more times
5
Pen up
6
Stop
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.
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 Rectangle
Instruction
1
Pen down
2
Move 50 steps
3
Turn right ? degrees
4
Move 20 steps
5
Turn right ? degrees
6
Repeat steps 2 to 5 ? more time
7
Pen up
8
Stop
3 and 5: turn right 90 degrees. 6: repeat steps 2 to 5 one more time. After the first pass the sprite has drawn two sides, so one more pass draws the other two sides.
2
Draw the flowchart for a main algorithm that calls Draw Rectangle, moves 80 steps with the pen up and calls Draw Rectangle again.
Start, then a rectangle ‘call: Draw Rectangle’, then ‘move 80 steps’, then ‘call: Draw Rectangle’, then stop. The pen is already up after the first call, so the move draws nothing.
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?
It draws a square. After four sides it is back at the start, and the last two repeats draw over the first two sides again. The fix is to turn 60 degrees.
4
Why should the sub-routine end with ‘pen up’? What would happen if it did not?
With the pen still down, the move between the shapes would draw a line joining them. Ending with pen up makes the main algorithm’s moves invisible.