← All articles

21 November 2026

What Are Functions in Robot Code? Teach the Robot Once, Reuse It a Hundred Times

Illustration of a code function block with its name and lines of code inside

Your child has just finished a program for a robot driving through a maze. It works — but there is a problem: every time the robot needs to turn left, the program repeats the same four lines of code: stop the right motor, run the left motor, wait a moment, then drive on. Four left turns means sixteen repeated lines. Want the turn a little faster? You have to edit all four places, and forgetting just one means the robot turns wrong at exactly the wrong moment.

There is a much tidier way: pack those four lines into a single name — say, turnLeft() — and then every time the robot needs to turn left, just call that name. That is a function: a name for a job, taught to the robot once and reused a hundred times.

What is a function, in plain words?

A function is a named chunk of code that does one specific job and can be called any time. Think of a recipe: instead of writing out all the steps for noodle soup from scratch every time you cook — boil water, add bones, season — you write "noodle soup" once in your notebook, then follow the recipe whenever you need it. A function in code is that notebook entry.

In robot code, a function usually has three parts: its name, the job it does, and sometimes extra information fed into it — called a parameter. The function turnLeft() has the name "turn left", its job is the four lines that drive the motors, and it needs no parameter because every left turn is the same. The function goStraight(50) is different: the parameter 50 tells the robot how far to go — 50 centimetres. Change the number to 100 and the robot goes twice as far, without rewriting the function.

Why do functions matter so much?

Functions do more than make code shorter. They change the way a child thinks about a problem — and that is the part that matters most.

Write once, use many times. This is the benefit you see first. A robot driving a maze might turn left twenty times in a single run. Without functions, the program is long and full of repeated chunks. With functions, the program becomes a short list of calls that reads like a to-do list: go straight, turn left, go straight, turn right.

Fix one place, fix everywhere. This is the biggest benefit of all. Want the robot to turn left faster? Edit the turnLeft() function once, and all twenty turns in the program get faster immediately. Without functions, you would have to find and edit twenty places — and missing just one means the robot turns wrong in exactly that one spot, at exactly the worst moment. Programmers have a famous saying for this: don't repeat yourself.

Break big jobs into small ones. A complete robot program — driving a maze, avoiding obstacles, finding the exit — sounds huge. But split into small functions, each doing one job, the big problem suddenly feels manageable: a go-straight function, a turn-left function, a turn-right function, an avoid-obstacle function, a check-if-we-are-out function. Children learn to look at a big job and ask: "What small jobs is this made of?" — that is decomposition, a core skill of every programmer.

Read code like a story. A program written with functions reads naturally: goStraight(50), turnLeft(), goStraight(30), avoidObstacle(). Even someone who has never learned to code can guess what the robot will do. Good code is code that other people can read — and functions are the strongest tool for writing code that way.

Parameters: one function, many uses

Parameters turn a function from "one fixed command" into "one flexible command". Imagine a blinkLight() function with no parameter: every call makes the light blink exactly once, always the same. Add a parameter times, making it blinkLight(5) — the light blinks five times. blinkLight(2) — twice. The same function, but each call gives a different result, depending on the number you pass in.

In robots, parameters are everywhere: goStraight(cm) — how far to go; turnBy(degrees) — how many degrees to turn; beepAt(hz) — what frequency the buzzer sounds at. Children soon spot a beautiful pattern: the fewer functions you need, and the more flexible each one is, the tidier the program becomes. One turnBy() function with an angle parameter replaces a 90-degree left-turn function, a 180-degree U-turn function, and a gentle 30-degree turn function all at once.

Some functions also "give back" a value — called a return value. The function measureDistance() does not move the robot at all; it measures the distance to the nearest object and returns a number, like 15 centimetres. The program uses that number to decide: if it is less than 10 centimetres, stop. The measuring function and the moving functions stay separate — each with one clear job, easy to check.

The first function your child should write

If your child is just starting, do not teach functions with theory. Let them discover the need for a function on their own — that moment usually arrives naturally.

Step 1: write repeated code. Have the robot drive a square: go straight, turn right, go straight, turn right, repeated four times. The code will be long, with four identical chunks. Do not fix anything — just let your child see the repetition.

Step 2: ask one question. "Do you see these four chunks are the same? If you want the robot to go faster, how many places would you have to change?" Your child will count: four places. And feel the annoyance.

Step 3: pack it into a function. Together, create two functions: driveOneSide() — drive straight along one side, and turnRight() — turn one corner. The square program is now just eight function calls, repeated four times. Change the speed? Edit one function only.

Step 4: level up. Add a parameter: driveOneSide(30) — drive a side 30 centimetres long. Now the same program can draw a big square or a small square just by changing one number. Your child has just learned functions, parameters, and reusable thinking — without anyone lecturing them on a single definition.

Another game that fits functions perfectly: "teach the robot to dance". Each dance move is a function — jumpUp(), spinAround(), wiggle() — then line the functions up into a routine. Change the order of the calls and you get a new dance. Children see that functions are like dance steps: learn each step once, then combine them into any routine you like.

From writing code to thinking like a programmer

There is a line every child learning to code will cross: from writing code as a stream of separate commands, to organising code into named functions. Crossing that line is the moment a child starts thinking like a real programmer.

Before the line, a child thinks: "What should the robot do next?" After the line, a child thinks: "What small jobs is this made of, and which ones will I use again?" The second question is harder, but it is the question every software engineer asks daily — whether writing code for a toy robot or for a large system.

Functions also teach a lesson about naming. A function called doThing1() says nothing; a function called avoidObstacle() says everything. Naming things clearly is a skill — and one children carry into every other subject: naming variables, naming files, naming a school presentation. Clarity in naming is clarity in thinking.

And once your child is comfortable with functions, bigger ideas arrive naturally: a library is a collection of functions someone else wrote; a maze-solving robot is really a chain of functions calling each other; and even a complicated robot is just many small functions put together, each one doing its own job well.


Start with ManuStem: Your child aged 8+ can learn robotics right in the browser — no kit to buy, nothing to install. 28 lessons including 8 Component Lab lessons and 20 Competition Robot lessons, 4 free trial lessons, and an AI co-pilot at every step: AI Circuit wires, Gen AI writes code, the Agent coach debugs, plus a 3D robot preview. Learn in Vietnamese and English. Explore now at stem.manusignal.com.

Read the Vietnamese version:

Hàm trong code robot là gì? Dạy robot một lệnh, dùng lại cả trăm lần