FutureBasic Logo

<<    Index    >> FutureBasic 5

def fn <expr>   statement

def fn functionName [(var1 [,var2 ...])] [as type] = 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:

def 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 (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 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.

def fn Area(r as single) as single = 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... 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.

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:
local fn; def fn <prototype>