Directives
DEF FN Define Function
   
Formats
1. Single Line: DEF FNname[$]([LOCAL ]argvar[,[LOCAL ]argvar][, … ])=expression[$]
   
2. Multi-Line: DEF FNname[$]([LOCAL ]argvar[,[LOCAL ]argvar][, … ])
RETURN expression[$]
END DEF
  Where:
argvar ... Comma-separated list of variables that correspond to arguments passed to the function or returned in the expression.
expression[$] String or numeric expression. In multi-line functions the expression is the value returned. In single-line functions, the expression defines the value of the function.
FNname[$] Name of the function, with "FN" prefix . Use a valid string or numeric variable name, e.g., FNX or FNABC$.
LOCAL Optional keyword. Indicates that an argument variable is local to the function. Use LOCAL to prevent permanent changes to program variables. ProvideX defers processing the LOCAL clause in functions until all arguments are parsed.
   
Description Use the DEF FN directive to define single- or multi-line functions. These functions are considered string or numeric depending on the type of variable used in the function name.

In Execution mode, ProvideX skips past the function without executing it. ProvideX resumes after the DEF FN directive, executing the rest of the code until it reaches the statement that invokes FNname( )... whereupon it applies the defined function.

   
Format 1 Single Line Function

DEF FNname[$]([LOCAL ]argvar[,[LOCAL ]argvar][, … ])=expression[$]

In a single-line function assignment, expression defines the value to be returned by the function. Include the optional keyword LOCAL to define argument variables as local.

   
Examples 0010 DEF FNX(A,B)=A^B
0020 LET A=1, B=2
0030 PRINT A,B,FNX(3,4),A,B
0040 STOP
->RUN
1 2 81 3 4

When FNX is invoked (as in PRINT FNX(3,4)), the values in the variable list are assigned to the variables in the function definition.



*Note* The variables in the function definition's argument list represent actual variables in the program. Their values are subject to change every time the function is invoked. Unless you define variables as LOCAL, changes to the variables in your program will be permanent. In the example above, variables A and B in the program are changed by FNX(3,4), because they aren't defined as LOCAL.


   
Format 2 Multi-Line Function

DEF FNname[$]([LOCAL ]argvar[,[LOCAL ]argvar][, … ])
RETURN expression[$]
END DEF

When ProvideX encounters a multi-line definition, execution skips the subsequent statements until an END DEF directive is found. (Again, the function is not executed until it is invoked.) Include the optional keyword LOCAL to define variables as local. The RETURN expression specifies the value to be returned by the multi-line function. To generate an error value in a multi-line function, use the ESCAPE directive followed by your given value.

   
Examples 0010 DEF FNX(A,B)
0020 IF A<0 OR B<0 ESCAPE 40 ! Variables' values must be >=0
0030 RETURN SQR(A^2+B^2) ! Length of hypotenuse
0040 END DEF
0100 PRINT FNX(7,8)
->RUN
10.63