Name a job once as a function, call it wherever it is needed, and see that every event block is a function too.
A game makes enemies at the start, on a timer and when a level begins. Writing the same blocks three times is how bugs creep in: name the job once instead.
Making an enemy takes several blocks: create it, place it, set its speed. When a game needs that job in several places, you can name the blocks once as a function, then call the function by name wherever you need it. Make one in Functions › Make a Function (in the toolbox, under Advanced). Its call block then appears in the same place.
Make your prediction first. Help opens once you've locked it in.

let bug: Sprite = null
function spawnBug () {
bug = sprites.create(img`
. . 7 7 . .
. 7 7 7 7 .
7 7 f 7 f 7
. 7 7 7 7 .
`, SpriteKind.Enemy)
bug.setPosition(0, randint(10, 110))
bug.vx = 40
}
scene.setBackgroundColor(9)
spawnBug()
spawnBug()
spawnBug()
function spawnBug holds the blocks for one bug. They run only when something calls spawnBug.
Count the bugs. Press the restart button under the simulator to watch again (the heights are random).

let bug: Sprite = null
function spawnBug () {
bug = sprites.create(img`
. . 7 7 . .
. 7 7 7 7 .
7 7 f 7 f 7
. 7 7 7 7 .
`, SpriteKind.Enemy)
bug.setPosition(0, randint(10, 110))
bug.vx = 40
}
scene.setBackgroundColor(9)
spawnBug()
spawnBug()
spawnBug()
The game: a bug flies in from the left every second. Dodge them.

let bug: Sprite = null
function spawnBug () {
bug = sprites.create(img`
. . 7 7 . .
. 7 7 7 7 .
7 7 f 7 f 7
. 7 7 7 7 .
`, SpriteKind.Enemy)
bug.setPosition(0, randint(10, 110))
bug.vx = 40
}
info.setLife(3)
let player = sprites.create(img`
. 4 4 .
4 4 4 4
. 8 8 .
. 8 8 .
`, SpriteKind.Player)
player.setPosition(140, 60)
controller.moveSprite(player)
player.setStayInScreen(true)
game.onUpdateInterval(1000, function () {
spawnBug()
})
sprites.onOverlap(SpriteKind.Player, SpriteKind.Enemy, function (sprite, otherSprite) {
otherSprite.destroy()
info.changeLifeBy(-1)
})
Ava's program:

let bug: Sprite = null
function spawnBug () {
bug = sprites.create(img`
. . 7 7 . .
. 7 7 7 7 .
7 7 f 7 f 7
. 7 7 7 7 .
`, SpriteKind.Enemy)
bug.setPosition(0, randint(10, 110))
bug.vx = 40
}
game.onUpdateInterval(1000, function () {
})
def spawn_bug():
global bug
bug = sprites.create(img("""..."""), SpriteKind.enemy)
bug.set_position(0, randint(10, 110))
bug.vx = 40
def on_update_interval():
spawn_bug()
game.on_update_interval(1000, on_update_interval)
Look at what Python did with on game update every: it is a def, a function, and the line after it hands that function to the game. Every event block you have used is a function the game calls for you.
The game runs for 10 seconds. The timer calls spawnBug every 1000 ms. How many times do the blocks inside spawnBug run?
This is the game in Python (press Python at the top of the editor). Match each Python line with what it does.
Score a point for every bug that is made: a survival score. Change the function, so every call scores.
Put the block inside the function, not inside the timer.
change score by 1 (Info) goes at the end of spawnBug.
Make A call spawnBug too. Does the extra bug score a point?
Drag in on A button pressed, and put call spawnBug inside it.
Score a point for every bug the game makes, by adding one block inside spawnBug.
This ticks by itself when your blocks do what is asked.
Make A send an extra bug.
This ticks by itself when your blocks do what is asked.

let bug: Sprite = null
bug = sprites.create(img`
. . 7 7 . .
. 7 7 7 7 .
7 7 f 7 f 7
. 7 7 7 7 .
`, SpriteKind.Enemy)
bug.setPosition(20, 30)
bug = sprites.create(img`
. . 7 7 . .
. 7 7 7 7 .
7 7 f 7 f 7
. 7 7 7 7 .
`, SpriteKind.Enemy)
bug.setPosition(80, 60)
bug = sprites.create(img`
. . 7 7 . .
. 7 7 7 7 .
7 7 f 7 f 7
. 7 7 7 7 .
`, SpriteKind.Enemy)
bug.setPosition(140, 90)
controller.B.onEvent(ControllerButtonEvent.Pressed, function () {
for (let value of sprites.allOfKind(SpriteKind.Enemy)) {
value.destroy()
}
})
There are 3 bugs on the screen. You press B. How many bugs are destroyed?
Make a coin collector that never runs out of coins:
Make a Function called makeCoin. Inside: set coin to sprite of kind Food, then set its position to random x and y.
Call makeCoin three times in on start.
In the overlap, after destroy and change score, call makeCoin once more.
A makeCoin function, called three times at the start and again each time a coin is collected.
This ticks by itself when your blocks do what is asked.
Snow falls in this globe: a function addFlake makes one flake, and a timer calls it. Make every flake fall faster (one change, in the function), then make A shake the globe: five extra flakes at once.
Students name a job (making an enemy) as a function, call it from several places, and see in Python that every event block is a function the game calls. About 40 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
A function is a named job: some blocks with a name, made once in Functions (under Advanced in the toolbox). Making it does not run it. It runs when a call block calls it, once per call. One copy serves every call, so a change to the function changes every place that calls it. The Python button at the top of the editor shows the same program as text: every event block (on game update every, on overlap, on A pressed) becomes a def, handed to the game, which calls it. MakeCode adds a global bug line inside the function: it means "use the bug variable from outside". Parameters (inputs) are next lesson.
Making a function does nothing until it is called. When nothing appears, ask which block calls the function.
Stretch: Make a second function, makeSpike, that adds an Enemy at a random place, and call it every 3 seconds.
Support: Give students the makeCoin function already made, and ask them to add the four calls.
Next lesson: a parameter, so one function can make a slow bug and a fast bug. Skillmap: the Game Maker Guide (arcade.makecode.com/skillmaps) builds bigger games from named jobs like these.