Thus when FN p(2,3) is being evaluated, a has the value 10 because it has been initialised in line 10, x has the value 2 because it is the first argument, and y has the value 3 because it is the second argument. Both x and y albeit also defined in line 10 are treated as LOCAL variables and their value doesn't modify their global counterparts. The result is then, 10+2*3=16.

When FN q() is being evaluated, on the other hand, there are no arguments. So a, x and y all still refer to the variables and have values 10, 0 and 0 respectively. The answer in this case is 10+0*0=10.

Now change line 20 to:

20 DEF FN p(x,y)=FN q()

This time, FN p(2,3) will have the value 10 because FN q will still go back to the variables x and y rather than using the arguments of FN p.

DEF FN can take parameters passed by REFerence as is obvious from the next example:

DEF FN ian$(REF
         jen$(),idx)=jen$(idx)

Some BASICs (but not NextBASIC) have functions called LEFT$, RIGHT$, MID$ and TL$.

LEFT$ (a$,n) gives the substring of a$ consisting of the first n characters.

RIGHT$ (a$,n) gives the substring of a$ consisting of the characters from nᵗʰ on.

MID$ (a$, n₁, n₂) gives the substring of a$ consisting of n₂ characters starting at the n₁ᵗʰ.

TL$ (a$) gives the substring of a$ consisting of all its characters except the first.

You can write some user-defined functions to do the same. For example:

10 DEF FN TL$(a$)=a$(2 TO )
20 DEF FN LEFT$(a$, n)=a$( TO
   n)

Check that these work with strings of length 0 or 1.

Note that our FN LEFT$ has two arguments, one a number and the other a string.

A function cannot have integer arguments, nor use integer expressions in its definitions.

NextBASIC functions within integer expressions

We already discussed the usage of the INT {...} keyword which converts any floating point expression into an integer expression, but in many cases this can be slow. In other cases the values produced by a function are either plain 8 or 16 bit integers which means that integer-only versions of said function would provide significant boost over their standard counterparts. NextBASIC caters for these cases with special integer-only forms of the following functions:

ABS nReturn the ABSolute value of n – See this Chapter
IN nRead value from Hardware Port n – See Chapter 22
INPUT nRead/Define input controllers – See Chapters 17 and 22
REG nRead value from Next Register n – See Chapter 22
PEEK aRead byte from address a in memory – See Chapter 23

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.