def fn <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:
This expression returns the value of
expr from the function definition.
The def fn <
expr> statement should not appear inside any local function.
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 (such as "as double", etc.) to indicate the data type that the function returns (and hence
expr should be of the same type). If none 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 (var1, 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 (parm1, parm2, etc.). These values are then assigned to var1, var2, etc., and are used in evaluating
var1, var2, etc. must be "simple" variables: they cannot be array elements, records, nor record fields. parm1, 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 begin globals...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 begin globals...end globals block) or are local to main.
single = pi * r * r
a = fn
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... as required.
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.
<expr> is a "non-executable" statement, which means you cannot affect the definition of the function by placing def fn
<expr> after then or else (in an if statement), nor by placing it inside any kind of "conditional execution" block such as if...end if, while...wend, for...next, etc. However, you can affect the function definition (at compile time) by placing def fn
<expr> inside a #if...#endif block.
def fn does not work for threaded functions.
local fn; def fn <prototype>