ABS x is another function whose argument x and result are both numbers. It converts the argument into a positive number (which is the result) by stripping the sign away, so that for instance:
ABS -3.2
is the same as:
ABS 3.2
which in turn equals 3.2
INT x stands for integer part – an integer is a whole number, possibly negative. This function converts a fractional number x into an integer by throwing away the fractional part, so that for instance:
INT 3.9
equals 3. Be careful when you are applying it to negative numbers, because it always rounds down: thus, for instance:
INT -3.9
will return -4
SQR calculates the square root of a number – the result that, when multiplied by itself, gives the argument. For instance:
SQR 4
returns 2 because 2*2 = 4
SQR 0.25
returns 0.5 because 0.5*0.5 = 0.25 and finally:
SQR 2
will return 1.4142136 (approximately) because 1.4142136*1.4142136=2.0000001
If you multiply any number (even a negative one) by itself, the answer is always positive. This means that negative numbers do not have square roots, so if you apply SQR to a negative argument you get an error A Invalid Argument.
You can also define functions of your own. Their names follow exactly what is valid for procedures. Their arguments/parameters follow the conventions about procedures as well. Additionally just like procedures and subroutines, functions can also be recursive. Recall the factorial example from Chapter 4 and let's try to express it in a function form
DEF FN factor(n)=n?(1,1,n*FN
factor(n-1))
ZX Spectrum Next User Manual, 3rd Edition (ISBN 978-1-5272-5496-1), written and illustrated by Phoebus R. Dokos. Copyright © 2020-2024 Phoebus Dokos / SpecNext Ltd. Licensed under CC BY-NC-SA 4.0. This is a transcription and can contain errors; check any doubt against the printed page.