Skip to main content

Lesson 1-3 · Introduction to Functions

Module 1 · Learning to Drive

Lesson 1-3 · Introduction to Functions

50–60 minBlockly FoundationReuse & abstractionSlides (PowerPoint)
👩‍🏫

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 — and say why the version with a return value isn't the one you want here
  • 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 block← square #1
straight block← cm: 30
turn block← Deg: 90
repeat block← square #2 — same thing again
straight block← cm: 30
turn block← Deg: 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.

An animation comparing two programs that each draw three 30 cm squares. Left, copy-paste: three identical Repeat loops, each with Straight 30 and Turn 90. Right, a function: one definition, to square, holding the loop, and three square call blocks with arrows to it. The plan changes to 40 cm squares. On the left the 30 is changed in the first loop, then the second, and the third is missed and circled in red. On the right it is changed once, in the definition, and all three calls light up. Below, what each robot now draws: the copy-paste program two 40 cm squares and one 30 cm square, the function program three 40 cm squares.
One change of plan, two programs. In the copy-paste version every copy has to be found and fixed; in the function version there is only one place to look.

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 block← rename to "square"
repeat block← repeat 4 times
straight block← cm: 30
turn block← Deg: 90
The definition: the square loop now lives inside a function named square.

You'll use return eventually — in Lesson 1-11, when you write Python functions that work something out and hand back the answer, like the turn angle for a polygon. But square doesn't compute an answer; it drives the robot. There's nothing to hand back, so there's no return.

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 block
square call block← the call block is just the function’s name
square call block
square call block
Three squares, three blocks. Everything about how a square gets drawn lives in the definition — the main program only says when.

Notice the call block carries no extra wording: it's labeled square, because that's the name you gave the function. Your own functions turn into blocks that sit in the palette alongside the built-in ones.

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?

Knowledge Check

You built your square function, but the call block that appeared is rounded and won't snap into your main program. What went wrong?

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 1-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 1-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​