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
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.

Sub-routinedefinedraw hexagonpen downmove30stepsturn ↻60degreesrepeat6pen upMain scriptwhenclickederase allgo to x:-100y:40draw hexagonmove80stepsrepeat3
Worked examples
StepWhat to do
1Open the My Blocks palette.
2Click Make a Block.
3Type the name, such as draw hexagon, and click OK.
4Build the steps under the new define block.
5Drag 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?
Sub-routinedefinehop twicechange y by20wait0.2secondschange y by-20wait0.2secondsrepeat2Main scriptwhenclickedgo to x:0y:0hop twicechange x by50hop twice
2
Aisha clicks the green flag, but nothing is drawn. Why?
Sub-routinedefinedraw petalpen downmove40stepsturn ↻60degreesmove40stepsturn ↻120degreesrepeat2pen upMain scriptwhenclickedgo to x:0y:0sayReady!for2seconds
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?
4
A main script calls a sub-routine 10 times. How many times are the blocks under the define block written?