DEF

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: DEF
Abbreviation: D Shift E 
Type: Command
Token code: 150/$96
Handling routine
in BASIC ROM:
46003–46048
$B3B3–$B3E0
List of all BASIC keywords


Remark: This article describes the BASIC command DEF in BASIC V2 on the Commodore 64.

Type: Command
General Programming-Syntax: DEF FN <function name>(parameter name)=<mathematical expression>

The BASIC-command DEF defines a function with exactly one single numeric argument which can be executed with FN afterwards. The definition may contain any legitimate mathematical expression consisting of mathematical and logical operands, functions and variables which finally results in a numeric value. Functions, operands and system variables such as ABS(), AND, ATN(), ASC(), COS(), EXP(), FN<function name>(), FRE(), INT(), LEN() LOG(), NOT, PEEK(), POS(), OR, RND(), SGN(), SIN(), SQR(), STATUS (ST), TAN(), TIME (TI) or VAL() are possible.
The expression could use the parameter's variable name which acts as a placeholder for the actual value when called by FN. Except for the variable with the same name of the function argument, all other variables can still be accessed.

DEF only works within program code. The DEF declaration has to fit into a single line. Afterwards it can be called with the command FN<function name>(<numeric argument>). The function name follows the same rules as for variable names. It begins with one letter (A-Z) and might optionally followed by letters or digits (0-9), but only the first two characters are significant. If you redefine an already defined function with the same name, the first definition will be overwritten and the new definition is the valid one.

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, argument name or any other syntactical deviation except for the expression itself, the program aborts with ?SYNTAX ERROR. The given expression is checked only when the function is actually called. The placeholder variable name is expected to be a simple numeric variable name otherwise, like for a string variable name, it leads to ?TYPE MISMATCH ERROR.

Examples[edit | edit source]

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)/LOG(10)
20 Y = 10
30 Y = Y * 10 : PRINT FN LO10(Y); " ";
40 IF Y<1E38 THEN 30

The following example gives a glimpse what is possible with functions defined by DEF. Several mathematical 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)

Double PEEK (get a pointer value from a memory location), also known as function DEEK() in other BASIC dialects:

10 DEF FN DP(A) = PEEK(A)+PEEK(A+1)*256
20 PRINT "BASIC PROGRAM LENGTH"; FN DP(49) - FN DP(43)

The function FRE is famously know for the bug which returns negative values if the result exceeds 32767. This can be fixed with the following function:

10 DEF FN FR(X) = FRE(0)-65536*(FRE(0)<0)
20 PRINT "MEMORY FREE:"; FN FR(0)

As a side-effect FRE() is called twice which could result in doubling the delay imposed by the Garbage Collection.


BASIC V2.0 (second release) Commands

ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | π | POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT