Futurebasic/Language/Reference/def fn expr

= DEF FN &lt;expr&gt; Statement =

Statement
✔ Appearance ✔ Standard ✔ Console

Description
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  from the function definition.

The  statement should not appear inside any   function.

can be any valid FB identifier which is different from any other function name defined in your program. can optionally end with a type-identifier suffix (such as,  ,  , etc.)

If  ends with a type-identifier suffix, the suffix indicates the data type that the function returns (and hence   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 (, , etc.) separated by commas and enclosed in parentheses, which immediately follows. Usually,  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 (, , etc.). These values are then assigned to,  , etc., and are used in evaluating

,, etc. must be "simple" variables: they cannot be array elements, records, nor record fields. ,, etc. can (with some exceptions) be any kinds of expressions, as long as the data type of each   expression is compatible with its corresponding   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 ...  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  functions) which contains one of those variables.

may contain other variables besides those which appear in the formal parameter list. All variables in  are either global (as declared within a  ...  block) or are local to main.

Example
The function  calculates the area of a circle, when the radius of the circle is passed as a parameter. We are assuming that the variable  is a global variable or a "local to main" variable whose value has previously been set to 3.14159...

When we call the  function, the value 113.079 gets assigned to the local variable. As a side effect of calling, the value of the "local to main" variable   is changed to 6.0.

Note
is a "non-executable" statement, which means you cannot affect the definition of the function by placing  after   or   (in an  statement), nor by placing it inside any kind of "conditional execution" block such as  ...,  ... ,  ... , etc. However, you can affect the function definition (at compile time) by placing   inside a   block.

does not work for threaded functions.