DEF FN

The DEF FN (DEFine FuNction) command can be used to add a total of 52 new functions to Sinclair BASIC; 26 numeric functions (A-Z) and 26 string functions (A$-Z$).

Owing to the internal structure of Sinclair BASIC, the best place to store these functions is in line 0. This prevents the line being accidentally edited or deleted and also gives the maximum possible speed (on a standard Spectrum with no Interface I attached, line 1 can be converted into line 0 with POKE 23756,0).

The more complicated or frequently called functions should be given the higher priority; for example, the MOD function should normally occur first.

Where possible, the following functions are compatible or partially compatible with their Beta BASIC equivalents. Some of the numeric functions are restricted to dealing with 8-bit numbers (0-255) while their Beta BASIC equivalents can handle 16-bit numbers. This is because dealing with 16-bit numbers in BASIC would be tortuously slow. Recursive functions are possible, but are not included here (in order to permit compilation with HiSoft BASIC).

hex4$ = 4 digit hex string ("0000"-"FFFF")

posint8 = 8-bit positive integer (0-255)

posint16 = 16-bit positive integer (0-65535)

string1 = non-null 1 character string

string2 = 2 character string

XOR(posint8,posint8)
This article began as a thread in the World of Spectrum Sinclair BASIC forum.