ZX 81 - BASIC Programming

Sinclair ZX Spectrum
Chapter 5.1 - Functions
Mathematically, a function is a rule for giving a number (the result) in exchange for another (the argument, or operand) & so is really a unary operation. The ZX81 has some of these built into it & their names are the words written under the keys.

SQR, for instance, is the familiar square root function, and

PRINT SQR 9

gives 3, the square root of 9. (To get SQR, you first press the FUNCTION key - shifted NEWLINE. This changes the cursor to . Now press the SQR key (H): SQR appears on the screen and the cursor changes back to . The same method works for all the words that are written underneath the keys, almost all of which are function names.)

Try

PRINT SQR 2

You can test the accuracy of the answer by

PRINT SQR 2*SQR 2

which ought to give 2. Note that both SQRs are worked out before the *, and in fact all functions (except one - NOT) are worked out before the five operations +, -, *, / and **. Again, you can circumvent this rule using brackets -

PRINT SQR (2*2)

gives 2.

Here are some functions (there is a complete list in appendix C). If your maths is not up to understanding some of these, it does not matter - you will still be able to use the computer.

SGN The sign function (sometimes called signum to avoid confusion with SIN). The result is -1, 0 or +1 according as the argument is negative, zero or positive.

ABS The absolute value, or modulus. The result is the argument made positive, so that

ABS -3.2 = ABS 3.2 = 3.2

SIN *
COS *
TAN *
ASN arcsin *
ACS arccos *
ATN arctan *
LN natural logarithm (to base 2.718281828459045..., alias e)
EXP exponential function
SQR square root
INT integer part. This always rounds down, so INT 3.9 = 3 & INT -3.9 = -4. (An integer is a whole number, possibly negative.)
PI = 3.1415265358979..., the girth in cubits of a circle one cubit across. PI has no argument. (Only ten digits of this are actually stored in the computer, & only eight will be displayed.)

RND Neither has RND an argument. It yields a random number between 0 (which value it can take) & 1 (which it cannot).

* The trigonometrical functions. These work in radians, not degrees.

Using the jargon of the last chapter, all these except PI & RND are unary operations with priority 11. (PI & RND are nullary operations, because they have no operands.)

The trigonometrical functions, & EXP, LN & SQR, are generally calculated to 8 digits accuracy.

RND & RAND: These are both on the same key, but whereas RND is a function, RAND is a keyword, like PRINT. RAND is used for controlling the randomness of RND.

RND is not truly random, but follows a fixed sequence of 65536 numbers that happen to be so jumbled up as to appear random (RND is pseudo-random). You can use RAND to start RND off at a definite place in this sequence by typing RAND, & then a number between 1 & 65535, & then NEWLINE. It's not so important to know where a given number starts RND off, as that the same number after RAND will always start RND off at the same place. For instance, type

RAND 1     & NEWLINE)

& then

PRINT RND

& type both these in turn several times. (Remember to use FUNCTION to get RND.) The answer from RND will always be 0.0022735596, not a very random sequence.

RAND 0

(or you can miss out the 0) acts slightly differently: it judges where to start RND off by how long the television has been on, & this should be genuinely random.

Note: In some dialects of BASIC you must always enclose the arguments of a function on brackets. This is not the case in ZX81 8K BASIC.

Summary

Statement: RAND

Functions: SGN. ABS, SIN, COS, TAN, ASN, ACS, ATN, LN, EXP, SQR, INT, PI, RND

FUNCTION

Sinclair ZX Spectrum

  Previous Page Back Next Page