Futurebasic/Language/Reference/def fn expr

DEF FN <expr> Statement

edit

DEF FN <expr>

edit

Statement

edit

✔ Appearance ✔ Standard ✔ Console

Syntax

edit

DEF FN functionName [(var1 [,var2 ...])] = expr

Description

edit

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.

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 suffix (such as %, $, &, etc.)

If 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 (var1, var2, etc.) separated by commas and enclosed in parentheses, which immediately follows functionName. Usually, 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 expr.

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.

Example

edit

DEF FN Area!(r!) = pi# * r! * r!

LOCAL FN Circle6
  a! = FN Area!(6.0)
  PRINT a!
END FN

The function 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.

Note

edit

DEF FN <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 LONG 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 COMPILE LONG IF block.

DEF FN does not work for threaded functions.

See Also

edit

LOCAL FN; END FN; USING; FN <userFunction>; @FN; DEF FN USING <fnaddress>