Week 14 · Unit 6: Sub-routines in programming · 3 lessons
3
Define a block in Scratch
Computing 6: Unit 6, pp. 78–91
GoalMake your own block in Scratch, build its script under the define block and call it from a main script.
New words
define · the Scratch block that holds the steps of a new blockMy Blocks · the palette where you make and find your own blockscall · to run a sub-routine by using its block in another scriptextension · a group of extra blocks that you can add, such as the Pen blocks
Explanation
In Scratch you make your own block in the My Blocks palette. Click Make a Block and type a name that says what the block does. A define block appears in the coding area, and the new block appears in the palette. The steps of the sub-routine go underneath the define block. To use the sub-routine, drag its block into another script. When the block runs, all the blocks under the define block run. You can use the new block as many times as you like. A define block does nothing on its own, because it only runs when another script calls it. The Pen blocks are in an extension that you add with the Add Extension button.
Worked examples
Step
What to do
1
Open the My Blocks palette.
2
Click Make a Block.
3
Type the name, such as draw hexagon, and click OK.
4
Build the steps under the new define block.
5
Drag the new block from the palette into the main script.
These are the steps for making the block. The name of the call block always matches the name that you typed.
Trace the program. The main script calls draw hexagon 3 times. Each call runs the move block 6 times, so the move (30) steps block runs 3 × 6 = 18 times. The move (80) steps block runs 3 times, once after each hexagon.
Ravi wants bigger hexagons. He changes move (30) steps to move (40) steps in the define script and, in the main script, move (80) steps to move (100) steps. All three hexagons change size, and he has changed only two blocks.
Class activity
Paper programs (no computer): write the blocks of a sub-routine ‘draw flag’ on strips of paper, with the define strip at the top, and put them on one sheet. Write the main script on another sheet: when flag clicked, go to x 0 y 0, draw flag (a different colour), move 90 steps, draw flag. An adult runs the program by pointing at each strip. When the pointer reaches ‘draw flag’ it jumps to the define sheet, runs the whole stack and then jumps back. Do it again with a friend pointing. If you have a computer or tablet, build the program in Scratch.
Practice
1
The program runs. What are the coordinates of the sprite at the end, and how many times does it move up?
The sprite starts at (0, 0). Hopping twice brings it back to y = 0, so it is still at (0, 0). Then it changes x by 50 and is at (50, 0). Hopping twice again leaves it at (50, 0). It moves up 4 times altogether.
2
Aisha clicks the green flag, but nothing is drawn. Why?
The define block is there, but the main script never calls ‘draw petal’. A sub-routine only runs when it is called.
3
Write a sub-routine ‘draw dash’ that draws a dash of 10 steps and then leaves a gap of 10 steps. Write a main script that draws a dotted line of 8 dashes. How long is the drawn part? How far does the sprite move?
Define draw dash: pen down, move 10 steps, pen up, move 10 steps. Main: when flag clicked, repeat 8 { draw dash }. The drawn part is 8 × 10 = 80 steps. The sprite moves 8 × 20 = 160 steps.
4
A main script calls a sub-routine 10 times. How many times are the blocks under the define block written?
Once. They are written once and run 10 times. This is why a change in the define script changes all ten calls.