HTBasic Help
×
Menu
Index

DEF FN

Begins a user-defined function subprogram.
 
 DEF FN function-name[$] [(parameter-list)]
 . . . statements
RETURN { numeric-expression | string-expression }
 . . . statements
FNEND
 
Usage:
DEF FNString$(@Path,REAL Array(*),OPTIONAL Factor$)
 
DEF FNNum(OPTIONAL X(*))
 
100  DEF FNFactorial(F)
110    IF F<0 THEN CAUSE ERROR 19
120    IF F<=1 THEN RETURN 1
130    RETURN F*FNFactorial(F-1)
140  FNEND
 
Example: DEFFN.BAS
 
Description:
When typing in a new user-defined function subprogram, the DEF FN must be the highest numbered line in the present program. The body of the function then follows. SUB or DEF FN statements are not allowed inside the body of the function. Lastly, the function definition is completed by a FNEND statement. Optionally, comments about the function can follow the FNEND statement. At least one RETURN statement must exist in the function definition. The RETURN statement specifies the value that is to be returned. The type of the value must match the type of the function name; a string function must return a string value and a numeric function must return a numeric value. If execution reaches the FNEND statement, an error will result.
 
When called, a list of arguments can be passed to the function and are associated with the DEF FNparameters. Parameters to the right of the OPTIONAL  keyword are optional and need not be passed in the argument list. An error results if the function attempts to use an optional parameter with no value passed to it. To avoid this, use NPAR to check the number of arguments passed to the function.
 
All variables defined in a subprogram that are not COM variables are local to the subprogram. Upon each entry to the subprogram they are set to zero.
A parameter may be used as a buffer if declared as a BUFFER in both the calling context argument list and the DEF FN parameter list. The variables of a parameter list cannot be declared in COM or other variable declaration statements.
 
Porting Issues
Nested I/O does not return an error under HTBasic but should not be used because future improvements may make it illegal. Using nested I/O also prevents the program from running under HP BASIC.
 
HTBasic limits the depth that recursion can occur. The depth is limited by the size of the processor stack, not the BASIC workspace size.
 
See Also: