Commodore‎ > ‎BASIC‎ > ‎Keywords‎ > ‎

DEF

Keyword Abbreviation Token (hex) Version(s) Classification
DEF D{Shift+E} 96 1.0 to 7.0 Statement

 
Syntax  
DEF FN name parameter ) = expression
 
Parameters Type Legal Value(s) Default Value Note(s)
name Literal character(s)
first (required): letter
others (optional): letter or digit
 
Can not be (or include) a keyword,
but it may be the same as a reserved variable!
parameter Literal character(s)
first (required): letter
others (optional): letter or digit
Can not be (or include) a keyword.
Can not be a reserved variable name.
expression BASIC text any mathematical expression
Should reference parameter.
May reference other variables, including reserved variables.

 
 
Purpose  
Define a custom numeric function.

 
 
Remarks  
This statement defines a numeric "user function"; it is always required before using the function.  It may only be entered as a statement in a program; attempting to enter it as a command generates ILLEGAL DIRECT ERROR.  Calling the function (see the FN keyword) may be done in direct mode, as long as the program containing the definition has been executed, the function definition is still in program memory, and variables have not been moved or cleared (see CLR and GRAPHIC), and program lines have not been edited (see DELETE and RENUMBER).
 
The name follows the usual rules for variable naming: the first character must be a letter, the remaining characters (if any) may be either a letter or digit, only the first two characters are significant, and the name may not be (or have embedded inside of it) any BASIC keyword.  The same rules also apply to the parameter, however it has the additional restriction that it can not be the same as a reserved variable.  This restriction does not apply to name because the FN keyword, which must precede it, identifies it as a "function type" so BASIC will never be confused with "floating-point type" reserved variables.
 
The expression may be any BASIC text that calculates a numeric value.  It should reference the parameter, although it is not required.  It may also reference other variables (user defined or reserved).  The parameter must be a floating-point type variable (no suffix).  Attempting to use a string or integer type of parameter will generate SYNTAX ERROR.  If the parameter does not already exist as a floating-point variable, BASIC will create it too (following the new "user function").  Any variables referenced in the expression will not be created when defining the user function (i.e., not when using DEF), but if they do not exist when the function is called, they will be created (with a value of zero).
 
After the function has been defined, it may be used anywhere BASIC accepts a numeric value, such as in a PRINT statement, in an IF condition, in a variable assignment, or as a parameter to another command or statement.  Unlike the similar DIM (for arrays), a function may be re-defined at anytime in a program.  The most recent definition will be used when called.  Note a user function may call another user function (i.e., nested functions), however no user function may call itself, either directly or indirectly.  Doing so will generate FORMULA TOO COMPLEX ERROR.
 
This has the potential to be a really fast "subroutine", in the case where you need to perform a simple calculation.  The two main problems are it is limited to numeric expressions (you can't make a function that returns a string) and it only accepts 1 parameter.  There is no way around the first problem.  For the second problem, a dorky way to deal with the limit is to assign values of extra parameters to variables before calling the function (note this doesn't work if you need to call the function multiple times in the same expression with different extra parameters).  I'm guessing because of one or both of these reasons, the DEF doesn't show up very often in BASIC programs on the CBM series.  This doesn't mean you shouldn't use it, it just means that it isn't very popular.
 
On the Plus/4 and C128, there is a documented bug with DEF/FN (so some might claim it is a "feature").  If the program memory is moved after the function has been defined (typically (de)allocating a bitmap), attempting to call the function will generally cause a SYNTAX ERROR. (In rare cases it might return a "random" value based on the re-located program text.)  This problem exists because the pointers to functions are not updated when the program memory is moved.  To avoid the bug when using bitmaps, allocate the bitmap before defining any functions.
 
 
Examples 1:
DEF FNY(X) = X*X

?ILLEGAL DIRECT ERRROR
READY.
NEW

READY.
10 DEF FNY(X) = X*X
20 PRINT FNY(5)
30 PRINT X
RUN
 25
 0

READY.
PRINT FNY(3)
 9

READY.
NEW
PRINT FNY(3)

?UNDEF'D FUNCTION ERROR
READY.
 
Example 2:
NEW

READY.
10 DEF FNCON(X) = 2*X+1 : REM name includes keyword "ON"
RUN

?SYNTAX ERROR IN 10
READY.
10 DEF FNC(X) = 2*X+1
20 C = 1 : X = 10 : REM has no effect on function
30 PRINT FNC(3)
40 PRINT C : PRINT X
RUN
 7
 1
 10

READY.
NEW

READY.
10 DEF FNF(X) = X*X+Y : REM emulate function of 2 parameters
20 Y = 100 : PRINT FNF(5)
RUN
 125

READY.
 
 
 
Compare With  
 
See Also  

© H2Obsession, 2014
Comments