ZX 81 - BASIC Programming

Sinclair ZX Spectrum
Chapter 6.1 - Variables
'My pocket calculator,' you will be saying, 'can store a number away & remember it later. Can your ZX81 do that?'

Yes. In fact it can store away literally hundreds, using the LET statement. Suppose that eggs cost 58p a dozen, & you want to remember this. Type

LET EGGS=58 (& NEWLINE, of course)

Now first, the computer has reserved a place inside itself where you can store a number, & second, it has given this place the name 'EGGS' so you can refer to it later. This combination of storage space & name is called a variable. Third, it has stored the number 58 in the space: we say that it has assigned the value 58 to the variable [whose name is] EGGS. EGGS is a numeric variable, because its value is a number.

Do you want to know how much eggs cost? Type

PRINT EGGS

If you want to know the cost of half a dozen eggs, then type

PRINT EGGS/2

In fact, should you want to know the square of the cosine of the price of one egg, you can type

PRINT COS (EGGS/12)**2

'How very easy,' you must think, & you will be wondering what to do next, when in rushes your housekeeper saying 'Glory be, eggs have just gone up to 61p a dozen.'

Well. There is no time to lose. Type

LET EGGS=61

This does not reserve any extra storage space, but replaces the old value of 58 with 61. Now you can type

PRINT EGGS

confident in the expectation of getting the most up-to-date price available.

Now type

PRINT MILK

You will get a report 2/0, & looking up 2 in appendix B, you will see that it means 'variable not found' - the computer hasn't the faintest idea how much milk costs, because you haven't told it. Type

LET MILK=18.5

& all will be all right.

(Type PRINT MILK again.)

A variable need not be named after groceries - you can use any letters or digits as long as the first one is a letter. You can put spaces in as well to make it easier to read, but they won't count as part of the name.

For instance, these are allowed to be the names of variables:

TWO POUNDS OF APPLES BUT NOT GOLDEN DELICIOUS
RADIO 3
RADIO 33
X
K9P

but these are not:

3 BEARS (begins with a digit)
TALBOT? (? is not a letter or a digit)
(inverse video characters not allowed)
FOTHERINGTON-THOMAS (- is not a letter or a digit)

Now type

CLEAR

&

PRINT EGGS

You will get report 2 (variable not found) again. The effect of CLEAR is to release all the storage space that had been reserved for variables - then every variable is as though it had never been defined. Turning the computer off & on will also do this - but then it doesn't remember anything at all when it is turned back on.

Expressions can contain the name of a variable anywhere they can have a number.

Note: In some versions of BASIC you are allowed to omit LET & just type in (say)

EGGS=58

This is not allowed on the ZX81. In any case, you'd find it rather difficult to type in.

Also in some versions, only the first two characters in a name are checked, so that RADIO 3 & RADIO 33 would count as the same name; & in some others a variable name must be a letter followed by a digit. Neither of these restrictions applies to the ZX81.

Yet again, in some versions of BASIC, if a variable has not yet appeared on the left-hand side of a LET statement then it is assumed to have a value 0. As you saw above with PRINT MILK, this is not so on the ZX81.

Summary

Variables

Statements: LET, CLEAR

Sinclair ZX Spectrum

  Previous Page Back Next Page