in BASIC ROM:
|List of all BASIC keywords|
Type: Command General Programming-Syntax: DEF FN <function name>(numeric argument)=<mathematical expression>
The BASIC-command DEF defines a function which can be executed with FN afterwards . The definition can consist of any mathematical operation. Consequently it can not only contain numeric expressions and mathematical operands, but also other functions or commands which result in a numeric expression such as ABS(), AND, ATN(), ASC(), COS(), EXP(), FN<function name>(), FRE(), INT(), LEN() LOG(), NOT, PEEK(), POS(), OR, RND(), SGN(), SIN(), SQR(), STATUS resp. ST, TAN(), TIME resp. TI or VAL().
DEF only works within program code. Within the DEF-Line the whole function is to be represented once. Afterwards it can be executed with the command FN<function name>(<numeric argument>). The function name begins with minimum one letter (A-Z), the following constituents of the name can be either further letters or numbers (0-9). If you redefine an already defined function with the same name, the first definition is overwritten and the new definition will be valid.
If you try to use DEF outside programs the BASIC interpreter will report an ?ILLEGAL DIRECT ERROR. If wrong characters are used in the function name, a ?SYNTAX ERROR IN LINE XYZ will be the result. If you use non-numeric elements within the definition (strings, wrong functions), a ?TYPE MISMATCH ERROR IN LINE XYZ will occur. An ?ILLEGAL QUANTITY ERROR IN LINE XYZ is also possible when wrong elements are combined. And of course you may not operate with divisors worth zero, otherwise the interpreter will halt and report ?DIVISION BY ZERO ERROR after you execute the function.
For a start a rather simple function that multiplies the variable X.
10 DEF FN FTEST1(X) = X*3 20 INPUT "Type in a number:"; A 30 PRINT FN FTEST1(A): PRINT 40 IF A<>64 THEN 20
In the next example, one numeric expression must be delivered, but is not accounted for afterwards, because only the variables A and B are calculated.
10 A=10: B=11 20 DEF FN C(X)= 3+(A*B)/2 30 D = FN C(0): PRINT D
This example defines the calculation of the logarithm of 10 in command line 10. In Line 30 the newly defined logarithm function LO10 is executed.
10 DEF FN LO10(X) = LOG(X)/LOG10 20 Y = 10 30 Y = Y * 10 : PRINT FN LO10(Y); " "; 40 IF Y<1E38 THEN 20
The following example gives a glimpse what is possible with functions defined by DEF. Several mathematic functions and commands are summarized.
10 A$="12.12.1964 SATURDAYEVENING" 20 DEF FN F1(X) = ( LEN(A$)/VAL(A$) ) *PEEK (53280) 30 PRINT FN F1(0) 40 DEF FN F2(X) = INT( ABS( FN F1(X)* 1E+8) / ASC(A$) ) 50 PRINT FN F2(1)