Contents / Part 1 / Previous chapter / Next chapter / Index


3 An introduction to variables

In the last section we made the computer do a number of calculations but it was never expected to remember any of the results after it had printed them out. Suppose that you have to calculate the wages for everyone in a company. After you have worked out each person's wage, it would he useful to be able to add it to all the other wages that you had worked out so far, so that in the end you would know the total wage bill. Keeping track of things that vary during a long calculation is done by using "variables".

Try typing this line into the computer

LET Z=5

And now try typing in each of the following, lines

PRINT Z+6

PRINT Z * 12

As you will have seen, once we have told the computer that "Z is 5" it understands that every time we use the letter Z in a sum it has to go and have a look to find out what the value of Z is (5 in this case) and use that number in the arithmetic that we set it to do. Now type in

LET Z=71

And then try these two lines

PRINT Z+12

PRINT Z/3

As you will gather the value of Z has changed from 5 to 7. In computer jargon "Z" is called a "numeric variable". That means that Z can be used to store any number, and you can change the value of Z any time you want to.

The computer is able to store hundreds of different variables and the variables don't just have to be called something as simple as Z, you can call a variable by as long a name as you want. For example you could write

MYAGE=30

Notice that MYAGE was written without any spaces between the word MY and AGE. There are only four restrictions about the names that we give to variables.

1 There must be no spaces in the middle of a variable name.

2 All variable names must start with a letter, though you can stick as many numbers in as you want to later on.

3 You must not use punctuation marks (like exclamation marks and question marks) in the variable name but you can use an underline character.

4 Variable names should not begin with BASIC "keywords" like PRINT and LET. One that is particularly easy to use by mistake is the keyword TO. However it is quite permissable to start a variable name with a lower case "to" because upper and lower case names are quite different. There is a full list of keywords starting on page 483.

To get lower case characters on the screen, make, sure that the CAPS LOCK is off by depressing it so that its light goes out. In this condition you will get small letters and numbers. Hold the SHIFT key down if you want to get just a few capital letters.

Any of the following variable names would be acceptable to the computer

LET AGE=38

LET this_year=1982

LET lengthOFrod=18

LET CAR_mileage=13280

LET value5=16.1

LET weight4=0.00135

LET chicken2egg3=51.6

However the (allowing variable names are illegal.

LET Football Result=3 [there's a space]

LET Who?=6 [there's a question mark]

LET 4thvalue=16.3 [starts with a number]

LET TODAY=23 [starts with TO]

LET PRINT=1234.56 [PRINT is a reserved word]

You will notice that in all the examples above we have put the word LET before the variable name. That gives a clear indication of what is actually happening inside the computer, namely that the numeric: variable "this_year", in one of the examples, is being given a new value "1982". The word LET is optional and the computer will understand perfectly well if we say

this_year=1982

This shortened version is much more common.

Exit: BBC Microcomputer User Guide; Kasoft Typesetting; Archer


The BBC Microcomputer User Guide was written by John Coll and edited by David Allen for the British Broadcasting Corporation.

Optical character recognition and original formatting effort by Mark Usher.

HTML version maintained by: Kade "Archer" Hansson; e-mail: archer@dialix.com.au

Last updated: Monday 12th February 2001