Teaching someone a dance usually goes like this: "Clap, clap, spin round, arms up!" By the third chorus nobody lists the moves again. They just shout "the greeting!" and everyone knows what to do.
Programmers do the same: they gather a group of steps, give it a name, and from then on only use the name. This named bundle is called a function (sometimes a procedure). Children of around 7 to 12 can grasp it with a few cards, a dance and a jug of lemonade, no computer needed.
What is a function? A child-sized explanation
A function is a named bundle of steps. It always has two parts:
- The definition: you write down once what the name means. "Greeting = clap, clap, spin, arms up."
- The call: whenever you need those steps, you just say the name. "Greeting!"
Your child already uses functions every day. "Brush your teeth" is really a list: wet the brush, add toothpaste, brush, rinse. Nobody reads that list out every evening; the name is enough. A computer, though, knows nothing at first, so someone has to write the definition before the name can be used.
Game 1: the family star dance
You need some paper cards and a pen.
- Pick the moves. Together, choose four moves and draw each one on its own card: clap, clap, spin round, arms up.
- Give the bundle a name. Draw a star on a new card. Line the four move cards up next to it. This is the definition: "Star = clap, clap, spin, arms up."
- Write a dance. Now make the main program with only a few cards: Star, hop, Star, sit down, Star.
- Run it. One person reads the cards in order, the other is the "dancer". Every time a star comes up, the dancer looks back at the definition and does all four moves. Then swap roles.
Then try the trick that makes programmers love functions: "What if we want a jump instead of the spin?" Change one card in the definition and run the dance again. The jump appears in all three places at once.
Count the cards before and after
Ask your child to count the cards. Writing the Star out in full takes four cards every time. Using the name takes four cards for the definition, once, plus one card per call.
| Times the Star dance is used | Written out in full | With the name |
|---|---|---|
| 1 | 4 cards | 5 cards |
| 2 | 8 cards | 6 cards |
| 3 | 12 cards | 7 cards |
| 5 | 20 cards | 9 cards |
| 10 | 40 cards | 14 cards |
Look at the first row together. If a group of moves is used only once, naming it costs one extra card. Functions pay off when the same steps come back again and again, so the habit to learn is: look for what repeats first.
Game 2: add a number, and make lemonade
Now let the call carry a number: "Star, 3 times" means do the whole bundle three times in a row. A number given at the moment of the call is what programmers call a parameter. The steps stay the same; only the number changes.
The kitchen is a great place to practice this. Write a recipe card called "Lemonade (glasses)" with the steps for one glass:
- Squeeze half a lemon into the glass.
- Add one spoonful of sugar.
- Fill the glass with water.
- Stir.
An adult cuts the lemons. Then call the function with different numbers and ask your child to work out what's needed before you start:
| Call | Lemons | Spoonfuls of sugar | Glasses of water |
|---|---|---|---|
| Lemonade (1) | half | 1 | 1 |
| Lemonade (2) | 1 | 2 | 2 |
| Lemonade (4) | 2 | 4 | 4 |
| Lemonade (5) | 2 and a half | 5 | 5 |
The recipe card never changed, only the number in brackets. A program drawing 100 trees works the same way: one set of steps, called with different numbers.
Two puzzles: spot what can be bundled
Puzzle 1. A floor robot gets this program: step, step, turn left, step, step, turn left, step, step, turn left, step, step, turn left. That's 12 cards. Can your child find a bundle, give it a name, and write the program with fewer cards? What shape does the robot draw?
Puzzle 2. A clapping game: clap, clap, stamp, clap, clap, stamp, spin, clap, clap, stamp, clap, clap, stamp, spin. That's 14 cards. Here, one bundle can contain another.
Answers. Puzzle 1: "Side = step, step, turn left" (3 cards) and the program "Side, 4 times" (1 card): 4 cards instead of 12. The robot walks a square, two steps along each side, and ends up back where it started, facing the same way. Puzzle 2: "A = clap, clap, stamp" (3 cards), "B = A, A, spin" (3 cards) and the program "B, B" (2 cards): 8 cards instead of 14. A function that uses another function is completely normal in programming.
Questions that help children think it through
- "Which part keeps coming back?" That is the part worth naming.
- "Does the name tell us what it does?" "Wave" is clearer than "Thing 2". Good names make a program easy to read.
- "What does this bundle need to know?" A number of times, a number of glasses, a color: those are its parameters.
- "If we change the definition, what else changes?" Everything that calls it, so check the definition carefully.
In Dologi, the harder "Repeat moves" puzzles in the Space topic have children put a few moves inside the bracket of a repeat block, with a number saying how many times the group runs: for a road shaped like stairs, one stair goes in the bracket. It isn't a named function yet, but it trains the same habit of spotting a repeating group and packing it together.
In short
- A function is a named bundle of steps: define it once, then call it by its name.
- Bundling saves the most when the same steps are used many times, and changing the definition in one place updates every call.
- A parameter is the number or detail given at the moment of the call, like "3 times" or "4 glasses"; the steps stay the same.
- Dances, recipes and robot paths are easy, screen-free ways to practice finding what repeats and giving it a name.
Read more: Loops and conditions for children: learning through skipping and songs
