qb45advr.hlp (Topic list)
DEF FN Statement Details
  QuickSCREEN      Details      Example      Contents      Index
──────────────────────────────────────────────────────────────────────────────
DEF FN Statement Details
 
Single-Line Syntax
  DEF FNname[(parameterlist)] = expression
 
Block Syntax
  DEF FNname[(parameterlist)]
     [statements]
  FNname = expression
     [statements]
  END DEF
 
  Argument        Description
  name            legal variable name, up to 40 characters long. This
                  name, combined with FN, is the name of the function.
                  The name can include an explicit type-declaration
                  character to indicate the type of value returned.
                  Names that are the same except for the type-
                  declaration character are distinct names. For
                  example, the following are names of three different
                  DEF FN functions:
 
                    FNString$
                    FNString%
                    FNString#
 
                  To return a value from a DEF FN function, assign the
                  value to the full function name:
 
                    FNString$ = "No answer."
 
  parameterlist   A list of variable names, separated by commas. The
                  syntax is explained below. When the function is
                  called, BASIC assigns the value of each argument to
                  its corresponding parameter. Function arguments are
                  passed by value. DEF FN functions do not accept
                  arrays, records, or fixed-length strings as
                  arguments.
 
  expression      In both syntaxes, expression is evaluated and the
                  result is the function's value. In Syntax 1,
                  expression is the entire body of the function and
                  is limited to one logical line.
 
                  When no expression is assigned to the name, the
                  default return values are zero for a numeric DEF FN
                  function, and the null string ("") for a string
                  DEF FN function.
 
A parameterlist has the following syntax:
 
  variable [AS type] [,variable [AS type]]...
 
A variable is any valid BASIC variable name. The type is INTEGER,
LONG, SINGLE, DOUBLE, or STRING. You may also indicate a variable's
type by including a type-declaration character (%, &, !, #, or $) in
the name.
 
  Note: The FUNCTION procedure offers greater flexibility and control
        than the DEF FN function. See the FUNCTION statement for more
        information.
 
You must define a DEF FN function with a DEF FN statement before the
function is used. Calling a DEF FN function before it is defined
produces the error message "Function not defined." DEF FN function
definitions cannot appear inside other DEF FN definitions. In
addition, DEF FN functions cannot be recursive.
 
You must use the EXIT DEF statement to leave a multiline DEF FN early.
DEF FN functions can only be used in the module in which they are
defined. They cannot be referenced from other modules.
 
A DEF FN function may share variables with the module-level code.
Variables not in the parameterlist are global--their values are shared
with the calling program. To keep a variable value local
to a function definition, declare it in a STATIC statement.
 
DEF FN can return either numeric or string values. DEF FN returns a
string value if name is a string variable name, and a numeric value
if name is a numeric variable name. Assigning a numeric value to a
string function name or assigning a string value to a numeric
function name produces the error message "Type mismatch."
 
If the function is numeric, DEF FNname returns a value with the
precision specified by name. For example, if name specifies a
double-precision variable, then the value returned by DEF FNname is
double precision, regardless of the precision of expression.
 
Because BASIC may rearrange arithmetic expressions for greater
efficiency, avoid using DEF FN functions that change program variables
in expressions that may be reordered. The following example may give
different results:
 
  DEF FNShort
     I=10
     FNShort=1
  END DEF
  I=1 : PRINT FNShort + I + I
 
If BASIC reorders the expression so FNShort is called after
calculating (I+I), the result is 3 rather than 21. You can usually
avoid this problem by isolating the DEF FN function call:
 
  I = 1 : X = FNShort : PRINT X + I + I
 
Doing I/O operations in DEF FN functions used in I/O statements
or doing graphics operations in DEF FN functions in graphics
statements may cause similar problems.