Directives 

DEF FN

Define Function

Formats

1. Single Line:

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

2. Multi-Line:

DEF FN name[$][([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. Alternatively, a function can be defined with no arguments.

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. PxPlus 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, PxPlus skips past the function without executing it. PxPlus resumes after the DEF FN directive, executing the rest of the code until it reaches the statement that invokes FN name( )... whereupon it applies the defined function.

If desired, a function can be defined with no arguments by leaving out the parenthesis:

Example:

def fnmyfunc
read record ("myFile",key="2020")data$
return data$
end def

x=fnmyfunc

Note:
The maximum number of recursive calls to a user-defined function is 100.

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.

Example:

def fnX(A,B)=A^B
A=1,B=2
print A,B,fnX(3,4),A,B
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 are not defined as LOCAL.

Format 2

Multi-Line Function

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

When PxPlus 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.

Example:

def fnX(A,B)
if A<0 or B<0 \
     then escape 40 ! Variables' values must be >=0
return sqr(A^2+B^2) ! Length of hypotenuse
end def
print fnX(7,8)

->run
10.63