DEF FN <expr> StatementEdit
DEF FN <expr>Edit
✔ Appearance ✔ Standard ✔ Console
DEF FN functionName [(var1 [,var2 ...])] = expr
This statement defines a "one-line" function. You can refer to the function in later parts of your program by using an expression in this form:
FN functionName [(parm1 [, parm2 ...])]
This expression returns the value of
expr from the function definition.
DEF FN <expr> statement should not appear inside any
functionName can be any valid FB identifier which is different from any other function name defined in your program.
functionName can optionally end with a type-identifier suffix (such as
functionName ends with a type-identifier suffix, the suffix indicates the data type that the function returns (and hence
expr should be of the same type). If no type-identifier suffix is specified, the function returns a long-integer value.
You can optionally include a formal parameter list in the function definition: this is a list of variable names (
var2, etc.) separated by commas and enclosed in parentheses, which immediately follows
expr will contain references to these parameter variables. When you call a function that has a formal parameter list, you pass values to it in an actual parameter list (
parm2, etc.). These values are then assigned to
var2, etc., and are used in evaluating
var2, etc. must be "simple" variables: they cannot be array elements, records, nor record fields.
parm2, etc. can (with some exceptions) be any kinds of expressions, as long as the data type of each
parm expression is compatible with its corresponding
var variable in the formal parameter list. The number and order of the items in the actual parameter list must exactly match the number and order of the items in the formal parameter list (if any).
The variables in the formal parameter list are either global (if they were previously declared within a
END GLOBALS section), or they are "local to main." In either case, this means that the values which get assigned to those variables (when you call the function) persist even after the function returns its value. You need to keep this in mind if you later execute some statement in "main" (outside of all
LOCAL functions) which contains one of those variables.
expr may contain other variables besides those which appear in the formal parameter list. All variables in
expr are either global (as declared within a
END GLOBALS block) or are local to main.
DEF FN Area!(r!) = pi# * r! * r!
LOCAL FN Circle6
a! = FN Area!(6.0)
FN Area! calculates the area of a circle, when the radius of the circle is passed as a parameter. We are assuming that the variable
pi# is a global variable or a "local to main" variable whose value has previously been set to 3.14159...
When we call the
Circle6 function, the value 113.079 gets assigned to the local variable
a!. As a side effect of calling
FN Area!(6.0), the value of the "local to main" variable
r! is changed to 6.0.
DEF FN <expr> is a "non-executable" statement, which means you cannot affect the definition of the function by placing
DEF FN <expr> after
ELSE (in an
IF statement), nor by placing it inside any kind of "conditional execution" block such as
NEXT, etc. However, you can affect the function definition (at compile time) by placing
DEF FN <expr> inside a
COMPILE LONG IF block.
DEF FN does not work for threaded functions.
LOCAL FN; END FN; USING; FN <userFunction>; @FN; DEF FN USING <fnaddress>