Reuse with sub-routines
Answers are for parents and teachers.
1
Write the sub-routine ‘Draw Rectangle’ for a rectangle 60 steps long and 30 steps high. Then write a main algorithm that draws two rectangles with a gap of 20 steps.
Sub-routine: pen down, move 60 steps, turn right 90, move 30 steps, turn right 90, repeat the last four steps one more time, pen up. The rectangle is 60 wide, so the main moves 80 steps with the pen up between them. Main: call Draw Rectangle, move 80 steps, call Draw Rectangle.
2
Find the steps that repeat and rewrite the algorithm with a sub-routine called ‘place one chair’. 1 Pick up a chair. 2 Carry it to the row. 3 Set it down. 4 Pick up a chair. 5 Carry it to the row. 6 Set it down. 7 Pick up a chair. 8 Carry it to the row. 9 Set it down.
Sub-routine ‘place one chair’: 1 Pick up a chair. 2 Carry it to the row. 3 Set it down. Main: place one chair, place one chair, place one chair (or repeat 3 times: place one chair). The main algorithm has 3 lines instead of 9.
3
Trace the program. Where does the sprite end?
The sprite starts at (-50, 30). Each slide adds 25 to x and subtracts 5 from y. After 4 slides: x = -50 + 100 = 50 and y = 30 - 20 = 10. It ends at (50, 10).
4
Tell someone at home what a sub-routine is in your own words. Give one example from daily life where a set of steps is used again and again.
A sub-routine is a set of steps with a name that can be used many times. Examples will vary, such as laying one place at a table for each person.
5
(if you have a computer or tablet) Build ‘draw dash’ in Scratch and draw a dotted line with the repeat block. Change the number of repeats and see what happens.
The dotted line gets longer or shorter. The sub-routine does not change, only the number of repeats in the main script.