You sit down to type in a non-Sinclair BASIC program into your ZX/TS only to find in the listing a FN(A) statement. Should you forget about the rest of the program? No. We’ll simulate a defining functions feature. This versatile approach is actually better than other micros’ function defining feature.
Assigning the function to a string and calling it by using the VAL command executes the function. Although the ZX/TS manual briefly explains the procedure, you can use and expand this ability to satisfy a variety of programming needs.
For those not familiar with the VAL command, it takes the actual numeric value of any numbers or numeric phrases in a string variable. The ZX/TS expression evaluator looks at the formula assigned to the string and performs all the incorporated calculations. Example: If your borrowed program contains these lines:
100 DEF FN(A)=X/.023*SIN(X-2)
200 LET X=X-FN(A)
change them to:
100 LET A$="X/0.023*SIN (X-2)"
200 LET X=X-VAL (A$)
Like other computers’ FN(A) statement, when you call the function on your ZX/TS the variables in the formulas must have some preassigned value; no 0 default exists for unassigned variables.
You can also put USR calls (machine language subroutine call) in the function without affecting the answer. Suppose you want to clear the screen each time you call a certain function. Try this demo program to see it work:
5 LET X=10
10 FOR S=1 TO 21
20 PRINT "[illegible]"
30 NEXT S
100 LET A$="X/0.023*SIN (X-2)+0*USR 2602"
105 LET A$=A$( TO 6)
110 LET B=VAL A$
120 PRINT "A$=";B
(RUN it in SLOW or you’ll miss the show.) Lines 10-30 fill the screen with black lines. Line 100 assigns a value to variable A$. Line 110 tells the computer to calculate the VALue of A$. It performs the math in the first part of the formula. When it comes to the USR 2602, it goes to the machine language routine at that memory location. The 8K ROM clear screen routine resides at location 2602. In ZX/TS BASIC, a USR command returns the contents of the BC registers. So if you enter 100 PRINT USR 2602 and RUN, the screen clears and whatever value is in the BC registers prints on the screen. So we multiply the USR command by 0 to nullify the number it would otherwise return.
Save time and space by including conditional statements in your functions. The ZX/TS expression evaluator scans the function, interpreting true conditionals as 1 and false conditionals as 0. You don’t have to check everything with IF-THENs. For example:
100 LET G=10
200 LET X=3
300 LET Y=G*X
400 LET A$=(Y<X)*2+2**(Y>(G))"
500 PRINT CHR$ VAL$ A$;"AAAA"
Since the first expression is false (30 is not less than 3), the ZX/TS evaluates it as 0 and multiplies 0 by 2. The second expression is true (30 is greater than 13), so it evaluates as 1. The computer raises 2 to the first power, yielding 2, and adds it to results of the previous operation, 0. Line 500 thus prints the character whose code is 2, the graphic symbol on the 2 key, before the string “AAAA”.
If you know ZX/TS BASIC fairly well, you probably understand its string slicing ability. Suppose A$=”ABCDEFG” and you want to print the first four characters of A$. You enter 100 PRINT A$( TO 4). Using this principle you can execute certain parts or sections of a function exclusively. For example, add to our previous program:
105 LET A$=A$( TO 6)
Now the computer evaluates only the first six characters of A$. Your answer in line 120 is different, and you don’t get the screen clearing, because everything after the first six characters is sliced off. Be sure not to slice a function after an operation symbol.
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.