Skip to content

A Place to Remember

A variable is a named box that holds a value, so a program can keep it, use it, and change it.

  • Foundations

A program often needs to use a value more than once: a score to show, a life count to check, or a player’s position to change. It needs somewhere to keep that value.

The tool for that is a variable, and almost everything from here on builds on it.

A named box

A variable is a box with a name. You put a value in the box. Later you write the name, and the computer looks inside.

Code
LET score = 0
SHOW "Score: ", score
Output
Score: 0

Two things happened there. LET score = 0 put the value 0 into a box called score. The next line used that box: where you write score, the computer reads whatever sits inside it.

Reading the value does not empty the box. The value stays there for the next time the program needs it.

The name is yours to choose. points or total would do as well. Use the same name every time you mean that box.

One thing to unlearn now. That = does not mean “equals” the way it does in maths. LET score = 0 is an instruction: put 0 into the box called score. The difference looks fussy today. In Working It Out it saves you a headache.

Change the box, keep the name

A box you can put a value into is a box you can put a different value into. The name stays. The contents change.

Code
LET score = 0
SHOW "Score: ", score
SHOW "Score: ", score
LET score = 100
SHOW "Score: ", score

Trace these instructions in order. What will each of the three SHOW instructions display? What value will score hold at the end?

Check your prediction
Output
Score: 0
Score: 0
Score: 100
StepscoreShown
10—
20Score: 0
30Score: 0
4100—
5100Score: 100

Steps 2 and 3 read the same value. Step 4 replaces it; step 5 reads the new value. At the end, score holds 100. The earlier output still says Score: 0: changing the variable does not rewrite what has already been shown.

That is the whole power of a variable: a name you read whenever you want the value, and write whenever you want to change it. A score that climbs, a life count that falls, a player who moves across the screen — each one is a box with a name, holding a value that changes as the program runs.

When it’s wrong, see why

  • The value never changes. Either you never put a new value in, or you spelled the name differently the second time. score and scor are two different boxes.
  • You get nothing where you expected a value. You used the box before you put anything in it. Fill it first, then use it.
  • The computer does not know the name. You asked for a box it has never seen. Check the spelling against the line that filled it.

What you’ve learnt

  • A variable is a named box that holds a value. It is the program’s memory.
  • You put a value in with LET, and use it by writing its name.
  • The contents change and the name stays. That is how a score climbs.
  • = in LET means put this in the box, not “equals”. Hold on to that.

What’s next

Right now you decide what goes in the box, back when you write the program. In Unit 5 the program asks the person using it.

You can try this idea by storing and changing a greeting in Sinclair BASIC in Meet BASIC. The lesson explains the Spectrum’s own syntax as you use it.