DEF on the Sinclair

See all articles from SYNC v2 n4
Author(s): Jon Passler
Publication: SYNC
Pub Details: SYNC v2 n4 (Volume 2, Issue 4)
Date: July/August 1982
Pages: 64-66

Occasionally a program listing such as “3-D Plot” from Creative Computing’s Basic Computer Games contains the DEFine statement which allows the programmer to define a function in the form

DEF FNA(X)=(expression)

FNA merely stands for FuNction A,” and the variable X in the parentheses following FNA is termed the “dummy argument.” Usually the expression to the right of the equals sign in the DEFine statement has at least one X in it. Of course, variable names other than A and X could be used.

A simple example would be the statement

DEF FNA(X)=X**2+X

Normally, when FNA is “called” or used within the program flow, an argument is given within the parentheses different from the dummy argument, but which replaces all occurrences of the dummy argument in the DEFine statement. FNA(2) would have the value of 2**2+2, or 6, while FNA(A+B) would be the result of (A+B)**2+A+B. FNA can be treated like any other numeric variable. The only difference is that it is the result of an expression DEFined at the start of the program, outside the program flow, and the programmer can alter the argument of the expression.

The definition could have contained more than one dummy argument or variables which are not dummy arguments, such as

FNB(F,G)=F*G-Q/R

Functions can also be used as arguments for other functions, for example

FNB(3,FNA(A+B))

would be the result of

3*((A+B)**2+A+B)-Q/R

The DEFine statement is useful to simplify equations or cut down on programming where one equation is used at several points within the program.

One way around DEFine in Sinclair Basic is to replace all FN calls with the expression in the definition. This often requires breaking down an equation to simplify it.

Another is to use the 8K VALue function which can evaluate a string such as “2” or “2+2” or even “X**2+X”. The solution then is to replace DEF FNA with

LET A$=(expression)

and replace the FN call with VAL A$. The only problem appears when something like FNA(A+B) appears. We cannot call the function and define the argument all in one statement. Generally, if several different arguments are used in the FN calls, it is best to set the dummy argument equal to the argument before calling for VAL A$, setting, for example, X=A+B.

Listing 1 is the program for “3-D Plot” from Basic Computer Games. Here FNA(Z) is defined in line 100 as

30*EXP(-Z*Z/100)

and in line 150 the argument is

SQR(X*X+Y*Y)

which replaces all Z’s in FNA. Line 150 also sets Z, which is not related to the Z in FNA(Z), to an integer value using the FNA call. This program is translated into Sinclair Basic (8K ROM, 1K RAM) in Listing 2. Here the string variable A$ is used to replace DEFine and set the argument equal to

SQR(X*X+Y*Y)

in line 145 before calling for VAL A$. A$ is an input since several different functions are plotted. The same results could be obtained by adding

146 LET A=30*EXP(-Z*Z/100)

and changing VAL A$ to A in line 150, except that line 146 would have to be edited each time the function is changed.

The functions are slightly altered to run on either 1K or 16K RAM. Neither program, however, will work correctly with defective 8K ROMs. The plots appear within a circular X-Y plane tilted about 30 degrees toward the viewer with the curve rising above or falling below the plane.

Try the following functions with the 1K program:

20*EXP(-Z*Z/100)
20*SIN(Z/10)-15
SQR ABS(150-Z*Z)*.4-2
20*(COS(Z/16))-5
20-20*SIN(Z/18)

In the 16K version all the five functions above are held in F$ (see Listing 3). Each function word such as SIN uses only one byte in F$. At line 180 a zero is POKEd into DF-SZ, which is the system variable with the number of lines (usually two) in the lower part of the screen. Entering a zero into it allows printing the function on the 24th line. An input cursor will crash the system if called for when there is no room for it; so lines 330-340 replace a dummy input to stop the program temporarily (press any key to continue).

This is a rather unusual example of an occasion when using a string variable to hold a function or functions can be useful. The technique would also be useful where a function is used several times within a program to facilitate the translation of a DEFine statement or to highlight a function at the start of a program.

To use the programs, enter Listing 2, press RUN and NEWLINE and then enter one of the five functions above and observe the results. If you have 16K, enter the expanded version in Listing 3, press RUN and NEWLINE. However, since the functions are already included in the program, you do not have to enter them again.

Listing 1: 3-D Plot, Original Program.

100 DEF FNA(Z)=30*EXP(-Z*Z/100)
110 FOR X=-30 TO 30 STEP 1.5
120 L=0
130 Y1=5*INT(SQR(900-X*X)/5)
140 FOR Y=Y1 TO -Y1 STEP -5
150 Z=INT(25+FNA(SQR(X*X+Y*Y))-.7*Y)
160 IF Z<=L THEN 190
170 L=Z
180 PRINT TAB(Z);"*"
190 NEXT Y
200 PRINT
210 NEXT X

Listing 2: 3-D Plot for Sinclair (8K ROM, 1K RAM)

100 INPUT A$
110 FOR X=-20 TO 20
120 LET L=0
130 LET Y1=5*INT (SQR (400-X*X)/5)
140 FOR Y=Y1 TO -Y1 STEP -5
145 LET Z=SQR (X*X+Y*Y)
150 LET Z=INT (25+VAL A$-.7*Y)
160 IF Z<=L THEN GOTO 190
170 LET L=Z
180 PLOT X+20,Z-5
190 NEXT Y
210 NEXT X
3-D plot screen display (1K version)
3-D plot screen display (1K version)

Listing 3: 3-D Plot for Sinclair (8K ROM, 16K RAM)

100 REM 3-D PLOT
110 DIM F$(5,16)
120 LET F$(1)="25*EXP (-Z*Z/100)"
130 LET F$(2)="25*SIN (Z/10)-15"
140 LET F$(3)="25*(COS (Z/18))-2"
150 LET F$(4)="25-25*SIN (Z/18)"
160 LET F$(5)="25*EXP (-COS (Z/16))-18"
170 FOR B=1 TO 5
180 POKE 16418,0
190 PRINT AT 23,0;F$(B)
200 PAUSE 99
210 FOR X=-25 TO 25
220 LET L=0
230 LET Y1=4*INT (SQR (625-X*X)/4)
240 FOR Y=Y1 TO -Y1 STEP -4
250 LET Z=SQR (X*X+Y*Y)
260 LET Z=INT (25+VAL F$(B)-.7*Y)
270 IF Z<=L THEN GOTO 310
280 LET L=Z
290 PLOT X+30,Z-8
300 PAUSE 10
310 NEXT Y
320 NEXT X
330 PAUSE 9999
340 IF INKEY$="" THEN GOTO 330
350 CLS
360 NEXT B
370 STOP
3-D plot screen display (16K version)
3-D plot screen display (16K version)

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.