Chapter 6 - 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
 
 

Exercises

1. Why do variable names (that is to say, names of variables) have to begin with a letter?
 
 

2. If you're unfamiliar with raising to powers (**, shifted H) then do this exercise.

    At its most elementary level, 'A**B' means 'A multiplied by itself B times', but obviously this only makes sense if B is a positive whole number. To find a definition that works for other values of B, we consider the rule

        A ** (B+C) = A**B * A**C

    You should not need much convincing that this works when B & C are both positive whole numbers, but if we decide that we want it to work  even when they are not, then we find ourselves compelled to accept that
 
 
        A**0 = 1
        A**(-B) = 1/A**B
        A**(1/B) = the Bth root of A
&
        A**(B*C) = (A**B)**C

    If you've never seen any of this before then don't try to remember it straight away; just remember that
 
 
        A**-1 = 1/A
&
        A**(1/2) = the square root of A

& maybe when you're familiar with these the rest will begin to make sense.

    Experiment with all this by telling the computer to print various expressions containing **: e.g.

       PRINT 3**(2+0),3**2*3**0

       PRINT 4**-1,1/4
 
 

3. Type

       LET E=EXP 1

Now E has the value 2.718281828..., the base of natural logarithms. Test the rule

       EXP a number = E ** the number

for various numbers.


Previous: Chapter 5    Next: Chapter 7