Lesson 3 · Introduction to Functions
Lesson 3 · Introduction to Functions
Teacher mode is on. Toggle it off (bottom-right) to see exactly what students see.
Your square program works — but what if you want to draw three squares? Copying the whole loop three times gets long and messy fast. In this lesson you'll learn to write the square code once, give it a name, and reuse it as many times as you like. That's a function.
Learning Objectives
By the end of this lesson you will be able to:
- Explain what a function is — a named, reusable block of code
- Define a function in Blockly using the "to do something" block
- Call your function from the main program
- Recognize when a function will make your code shorter and easier to change
The copy-paste problem
Here's how you'd draw three squares without a function — the same loop, pasted three times:
← square #1
← cm: 30
← Deg: 90
← square #2 — same thing again
← cm: 30
← Deg: 90
If you later want bigger squares, you have to change the number in three places and hope you don't miss one. A function fixes that.
Define once, call many times
A function has two parts, and keeping them straight is the whole lesson:
- Define — where you write what the function does. In Blockly this is the
"to do something" block; rename it to
squareand drop your Repeat loop inside it. - Call — where you use it. A new square block appears in the Functions palette; drag it into your main program wherever you want a square.
← rename to "square"
← repeat 4 times
← cm: 30
← Deg: 90

Your main program then becomes short and readable — wait for the button, then
call square as many times as you want:
Wait for button press
call square
call square
call square
What's the difference between defining a function and calling it?
Your program calls square (a 4-sided loop) four times. How many sides does the robot drive in total?
Activity · Build reusable shapes
Say it in English first. Before you build anything, write one or two plain
sentences on paper describing what your function should do — for example:
"square: repeat 4 times — drive 30 cm, then turn 90°." If you can't
describe it in words, you're not ready to build it in blocks. Do the same for
triangle before step 3.
- Take your square loop from Lesson 2 and move it inside a new function named
square. - In the main program, call
squarea few times and watch the robot retrace the same square each time. - Now make a second function,
triangle— Repeat 3, and work the turn angle out with the rule from Lesson 2 rather than looking it up. Call both in a sequence.
You want bigger squares everywhere in your program. With a square function, how many places do you change the size?
Real-world connections
"Write it once, use it everywhere" is how all real software is built:
Buttons & menus
An app defines a "button" once, then reuses it thousands of times across every screen.
Spawning enemies
One "make an enemy" function, called over and over, fills a whole level.
Skill libraries
Big robots are built from reusable skills — "pick up", "drive to" — called in different orders.
Wrap-up
- Why put repeated code in a function? (Write once, reuse, and change it in one place.)
- What are the two parts of using a function? (Define it, then call it.)
- Does defining a function make the robot move? (No — only calling it does.)