Introduction
15 minLook back at the farm you built in Lesson 9.
Three enclosures, and three near-identical blocks of program: fill the walls, cut the lid, spawn the animals. You wrote the same recipe three times with different numbers, and if you had wanted six pens you would have written it six times.
A loop cannot help you here. A loop does the same thing over and over in one place. What you want is to do the same thing in different places, at different times, mixed in with other things.
That is a function.
A function is a name for a group of blocks
You take a set of blocks that does one useful job, put them in a box, and write a name on the lid. From then on, that name is the job.
function
buildPen— fill the walls, cut the lid, spawn the cows
And wherever you want a pen, you write one block:
call
buildPen
The blocks still run. They are simply somewhere else, under a name, instead of cluttering up the middle of your program.
Why this is worth the trouble
Three things change the moment you name something:
- Your program becomes readable. A chat command that says call buildShell, call buildRoof, call cutDoors explains itself to someone who has never seen it.
- A fix happens once. Your pen has a bug? Fix the function. Every pen in the world is fixed, including ones you have not built yet.
- You can build with bigger pieces. A function can call another function, so
buildFarmcan be three calls tobuildPenand nothing else.
Functions can take a number, too
A function that always builds the pen in the same place is not much use — three calls would stack three pens on top of each other.
So a function can be given a number when you call it, exactly as a chat command can: click the (+) on the function and it grows a parameter. Then buildPen becomes build a pen here, and where "here" is depends on the number you hand it.
Loops and functions are not rivals
Keep the difference straight:
| Use it when | |
|---|---|
| loop | the same thing, many times, one after another |
| function | a job worth naming, used wherever you like |
And they work together: a loop that calls a function is one of the most useful shapes in programming. You will write one today.
Warm-up — a recipe with a name
A recipe book does not spell out "beat two eggs, add flour, add milk" every time it needs a pancake. It says pancake batter, once, and then refers to it by name for ever after.
Nobody thinks that is a trick. It is just what naming things is for.
What you will be able to do
- Make a function, give it a sensible name, and call it.
- Give a function a number so it can do its job in different places.
- Build a function out of other functions.
- Rewrite a long program so that it reads like a list of what it does.
Basic
20 minFollow along on your own computer. Everyone should finish this part with the same working program.
Step 1 — Make your first function
- Press
Cand open your project. - Click the Functions drawer.
- Click Make a Function….
- Change the name from
doSomethingtobuildPen, and click Done.
A new block appears in the workspace — an empty container with your name on it. It is not attached to any chat command, and it will not run on its own.
Step 2 — Put the job inside it
Drag the pen-building blocks from your Lesson 9 farm into the function's mouth:
Nothing has changed about what those blocks do. They are simply in a box with a name on it now.
Step 3 — Call it
Go back to the Functions drawer. There is now a call buildPen block in it. Drag one into a chat command:
Type pen in the world. A pen with five cows, from a one-block program.
Step 4 — Three calls, one place
Now try the obvious thing and watch it fail:
Type farm. You get one pen, not three — because all three calls build in exactly the same place, on top of each other.
The function needs to know where.
Step 5 — Give the function a number
Challenge 1
15 minName your house.
- Press
Cand open yourhousecommand from Lesson 8. - Click Make a Function… and create a function called
buildHouse. - Move every building block out of the chat command and into the function, so the chat command contains nothing but a single call.
- Run it and check the house is exactly as it was.
- Now add a second call to the same function in the chat command, and make the two houses stand side by side rather than on top of each other.
Step 5 needs the function to know where to build. You met the block that does that in Basic.
It works if: typing the command builds two identical houses side by side, the instructions for a house are written down exactly once in the whole program, and the chat command is only two blocks long.
Challenge 2
20 minA tower to any height.
A function can take a number for anything, not just position.
- Press
Cand make a function calledbuildTower. - Give it two number parameters: one for how far along it stands, one for how tall it is.
- The function builds a hollow tower at that offset, of that height, in a material of your choosing.
- Write a chat command that calls it three times to build three towers of three clearly different heights, side by side.
- Type it, then stand back and look at the skyline.
It works if: three towers of visibly different heights stand side by side without overlapping, and the instructions for building a tower appear exactly once in your program.
Challenge 3
25 minA village made of names.
Build something big out of pieces you have named.
- Press
Cand start a new chat command calledvillage. - Write at least three functions, each doing one clear job — for example a house, a pen with animals in it, and a path or a wall.
- Write one more function that builds a whole block of the village by calling the others.
- The chat command itself must contain no fill blocks and no spawn blocks — only calls.
- Use a loop somewhere, so that at least one thing is built several times without being written out several times.
- Type
villageand walk through it.
It works if: a village of at least six separate structures appears, the chat command contains only function calls, and someone reading just that chat command could tell you roughly what gets built.
Extra challenge
spare timeRead it out loud.
Go back to the longest program you have written — the farm from Lesson 9, the village from Lesson 10, or your Challenge 3 above.
- Rewrite it so that the chat command is five blocks or fewer, and every one of them is a function call.
- Every function must have a name that says what it does, in words a person would actually use —
buildShell,cutDoors,releaseAnimals. - No function may be longer than about ten blocks. If one is, it is doing more than one job — split it and give each half its own name.
- Now find someone who has not seen your program. Show them only the chat command, and ask them what it builds.
It works if: they can tell you roughly what the program does from the chat command alone, without opening a single function — and the world still builds exactly as it did before you started.
Why this is the real lesson
You have not added a single feature. The world builds precisely what it built before.
What changed is that the program now explains itself. That is not decoration: in three weeks you will come back to this project, and the version that says call buildShell will take you a minute to pick up, while the one with sixty fill blocks in a row will take you an afternoon.
The next lesson is a project with a lot of moving parts. Go into it with this habit and it will be comfortable. Go in without it and it will be a wall of blocks.