Creating Custom Blocks in Scratch
A middle-school Scratch coding lesson on custom blocks: learn to make your own blocks with My Blocks, add inputs, avoid repeating code, and keep projects tidy, with worked examples and a quiz.
Key takeaways
- A custom block is your own block that runs a group of instructions
- Custom blocks live in the My Blocks section and start with a 'define' block
- They let you reuse code instead of copying the same blocks again and again
- Inputs make a block flexible by letting you pass in different values each time
Why make your own blocks?
In Scratch, you snap together blocks to build a program. But after a while you notice something annoying: you keep building the same group of blocks over and over.
Imagine you want your sprite to draw a square three times in different spots. Each square needs four "move" and four "turn" blocks. That is the same eight blocks copied three times — twenty-four blocks just to draw three squares! If you find a mistake, you have to fix it in all three copies.
A custom block solves this. It lets you take a group of instructions, give it a name, and reuse it whenever you want. This is Scratch's version of a function, the same idea you can read about in Functions Explained.
Making your first custom block
Custom blocks live in a special part of the palette called My Blocks. Here is how to make one:
- Click the My Blocks category (the pink one at the bottom of the palette).
- Click Make a Block.
- Type a name, like
draw square. - Click OK.
Scratch does two things. It adds a new pink block named draw square to your palette, and it drops a special define draw square block onto your code area.
The define block is the recipe. Whatever instructions you place underneath it are what your new block will do.
A worked example: the draw square block
Let's fill in our draw square block so it draws a square with the pen. Place these blocks under the define block:
define draw square
pen down
repeat 4
move 100 steps
turn ↻ 90 degrees
pen up
Read it carefully. The repeat 4 loop runs four times. Each time, the sprite moves forward 100 steps and turns 90 degrees. Four sides, four turns — that makes a square. (If loops are new to you, see Loops and Repeats.)
Now the magic happens. To draw a square, you only need one block:
when green flag clicked
go to x: -100 y: 0
draw square
When Scratch reaches draw square, it jumps into the define block, runs all the instructions there, then comes back and carries on. One tidy block does the work of six.
Reusing your block
The whole point is reuse. Want three squares in different places? Easy:
when green flag clicked
go to x: -150 y: 0
draw square
go to x: 0 y: 0
draw square
go to x: 150 y: 0
draw square
Three squares, and the square-drawing logic is written only once. If you decide a square should have sides of 80 steps instead of 100, you change it in one place — the define block — and all three squares update. That is a huge time-saver and it stops bugs from sneaking in.
Adding an input to make it flexible
Right now every square is exactly the same size. What if we want squares of different sizes? We add an input.
An input is a slot in the block that you fill in each time you use it. To add one:
- Right-click your
draw squareblock and choose Edit. - Click Add an input (number or text) and name it
size. - Click OK.
Your define block now shows a size bubble you can drag into your code. Use it in place of the fixed number:
define draw square (size)
pen down
repeat 4
move (size) steps
turn ↻ 90 degrees
pen up
Now the block has a slot. When you use it, you type in the size you want:
when green flag clicked
draw square (50)
draw square (100)
draw square (150)
This draws a small square, a medium square, and a large square — all from one definition. The size value you type gets passed in and used wherever (size) appears. This is exactly how parameters work in real languages like in Python Functions and Parameters.
A second example: a custom block with two inputs
Let's make a block that makes the sprite say something a chosen number of times. Make a block named chant with two inputs: a text input called word and a number input called times.
define chant (word) (times)
repeat (times)
say (word) for 0.5 seconds
Now you can call it in lots of ways:
chant [Hello!] (3)
chant [Go team!] (5)
The first call makes the sprite say "Hello!" three times. The second makes it say "Go team!" five times. Same block, different behavior, just by changing the inputs.
How Scratch runs a custom block
It helps to picture what happens. When Scratch reaches your custom block in a script, it:
- Pauses the current script at that spot.
- Jumps to the matching
defineblock. - Copies any input values into the input slots.
- Runs all the instructions under
define, top to bottom. - Returns to where it left off and continues.
Because there is only one define, every use shares the same recipe. Change the recipe once, and every use changes.
Try it yourself
Build a custom block called draw polygon with two inputs: sides and length. The trick to a polygon is the turn angle: it is always 360 / sides degrees.
define draw polygon (sides) (length)
pen down
repeat (sides)
move (length) steps
turn ↻ (360 / sides) degrees
pen up
Now test it:
draw polygon (3) (100)
draw polygon (5) (80)
draw polygon (8) (60)
You should get a triangle, a pentagon, and an octagon — all from a single block!
Challenge: add a third input called color, set the pen color to it inside the define block, and draw a rainbow of shapes across the stage. When you can do that, you have truly mastered custom blocks.
Quick quiz
Test yourself and earn XP
Where do you create a custom block in Scratch?
Custom blocks are made and stored in the My Blocks section of the block palette.
What block holds the instructions for your custom block?
When you make a custom block, Scratch creates a matching 'define' block. The instructions you place under it run whenever the block is used.
Why are custom blocks useful?
Custom blocks let you write a group of instructions once and reuse them anywhere, so you avoid copying and pasting the same blocks.
What does adding an input to a custom block do?
An input is a slot in your block. Each time you use the block you can give it a different value, making the block flexible.
If you change the code inside one 'define' block, what happens?
There is one definition shared by all uses, so editing it updates the behavior everywhere the block is used.
FAQ
Yes, they are Scratch's version of functions. A custom block has a name and a set of instructions, and you can 'call' it by clicking or placing the block, just like calling a function in Python or JavaScript.
Yes. Inside one 'define' block you can place other custom blocks. This lets you build bigger ideas out of smaller, well-named pieces.
Keep exploring
More in Coding