Step 6
Variables: boxes that keep a number
New to lesson cards? See what a lesson card is.
So far you have typed every number right into a block: forward 100, right 90. That works, until you want a bigger picture. Then you hunt for every number that meant “the size” and change each one, and you miss one.
There is a better way. Keep the number in one place, and let every block ask for it. A place that keeps a value like this has a name in programming: a variable. It is called a variable because the value in it can vary: it can change while the program runs.
Variables in TurtleLab are boxes
TurtleLab draws a variable as a box, because a box is a good way to think about one:
- A box has a name on its lid. A variable has a name too.
- A box holds one thing. A variable holds one value.
- You can look inside a box whenever you like, and looking does not empty it.
- If you put something new in, the old thing is gone.
TurtleLab has four variables, named box1, box2, box3 and box4. They live in the yellow Boxes palette, and two kinds of block use them:
store in box1is a command, likeforward. It has a number slot on its right, and the number in that slot is what goes into the box.box1is a round yellow block. It stands for whatever number is in the box right now. You can plug it into any number slot, anywhere you could have typed a number.
Watch a variable change
The frame below is a real TurtleLab, and its program is slow on purpose, so you can see every block run. The block that is running lights up. The yellow strip at the bottom right of the drawing shows what is in each box. The Output box at the top right shows what a print block says. (Press Run to watch it again, or Start over.) Nothing you do in the frame is saved, and Open full size opens it in a tab of its own.
First look at the blocks, and predict before you watch:
- The first
store in box1has a 40 in its slot. What is inbox1after it runs? - The next two
store in box1blocks each have a+block in their slot:box1plus 10. What is inbox1after both have run? - The two
printblocks come next. What does each one print? Does printing change what is in the box? - The next
store in box1has a 100 in its slot. What happens to the number that was in the box before? - The last block is
forwardwith thebox1block in its slot. How long is the line the turtle draws?
Now press Run and watch the strip and the Output box. Were you right?
Check your answers
- 40.
-
- The first
+block works out 40 plus 10, which is 50, and stores it. The second works out 50 plus 10 and stores 60. Each time, the block works out the answer first, and only then puts it in the box.
- The first
- Each prints 60. Printing only looks inside the box. It does not change it.
- The 60 is gone. A variable holds one value, so storing a new one replaces the old one. The box now holds 100.
- 100, because
box1stands for the number in the box, and that is 100 now. (The Output box shows 60, 60 and 100, from the threeprintblocks.)
That is everything a variable does:
- It holds one value.
- Storing a value replaces whatever was there.
- Looking at it (using the
box1block) does not change it.
Try it: a house from one variable
In this frame, one variable sizes a whole house. Change a number, press Run, and look at the drawing.
Work through these, one at a time:
- Find the
store in box1block. What number is in its slot? The yellow strip shows what is in each box. - Count the
forwardblocks that have the yellowbox1block in their slot. How many are there? - Change the 80 in
store in box1to 120 and press Run. How long is every side of the house now? - Change it to 50 and press Run. How many numbers did you have to change?
Check your answers
- 80.
- Three: the
forwardin the walls, theforwardthat climbs to the roof, and theforwardin the roof. -
- The walls and the roof are all 120 long, because they all ask the same variable.
- One. Without a variable you would have had to find and change three numbers, and the house would look wrong if you missed one.
A variable can do the math too
In Step 2 you found that turn = 360 ÷ number of sides. Here the program works that out by itself, because the number of sides is in a variable.
- Change the 5 in
store in box1to 3, then 6, then 12. Press Run each time. - Find the two blocks that use
box1. What does each one do with it? - Why does one number in one variable make the shape close up every time?
Check your answers
- The
repeatuses it as the number of sides. The turn uses it inside the÷block, as360 ÷ box1. - Both blocks use the same variable, so they can never disagree. If you typed the sides and the turn in separately, you could give them numbers that do not go together, and the shape would not close.
Build one of your own
Open TurtleLab in a new tab and build there. The frames above do not keep your work.
Make a program that draws a square whose size is kept in a variable. You need store in box1 and box1 (Boxes palette), and repeat, forward and right (Flow and Turtle palettes). Run it. Then change one number and run it again to get a bigger square.
Save your work
Press the PDF button to keep a copy of your blocks and your drawing.
Done early?
Use two variables: box1 for the size and box2 for the number of sides. Make a shape that can be any size with any number of sides by changing only the two stored numbers. You will need a ÷ block (Math palette) for the turn.