Skip to main content

Lesson 3 · Introduction to Functions

Module 1 · Learning to Drive

Lesson 3 · Introduction to Functions

50–60 minPhase A · Blockly FoundationReuse & abstraction
👩‍🏫

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:

repeat blocksquare #1
straight blockcm: 30
turn blockDeg: 90
repeat blocksquare #2 — same thing again
straight blockcm: 30
turn blockDeg: 90
Repetition like this is a signal you want a function.

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 square and 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.
function_def blockrename to "square"
repeat blockrepeat 4 times
straight blockcm: 30
turn blockDeg: 90
The definition: the square loop now lives inside a function named square.

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
Knowledge Check

What's the difference between defining a function and calling it?

Knowledge Check

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.

  1. Take your square loop from Lesson 2 and move it inside a new function named square.
  2. In the main program, call square a few times and watch the robot retrace the same square each time.
  3. 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.
Knowledge Check

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:

Apps

Buttons & menus

An app defines a "button" once, then reuses it thousands of times across every screen.

Games

Spawning enemies

One "make an enemy" function, called over and over, fills a whole level.

Robotics

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

Resources