Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
DEF FN Statement Details
◄Summary► ◄Details► ◄Example► ◄Contents► ◄Index► ◄Back►
──────────────────────────────────────────────────────────────────────────────
Syntax 1:
DEF FNname[(parameterlist)] = expression
Syntax 2:
DEF FNname[(parameterlist)]
[statementblock]
FNname = expression
[statementblock]
[EXIT DEF]
[statementblock]
END DEF
Usage Notes
■ In both versions of syntax, the argument expression is evaluated and
the result is the function's value. In Syntax 1 (single-line syntax),
expression is the entire body of the function and is limited to one
logical line.
■ When the function is called, Visual Basic assigns the value of each
argument to its corresponding parameter. Function arguments are
passed by value. Functions defined by DEF FN do not accept arrays,
records, or fixed-length strings as arguments.
■ When no expression is assigned to the name, the default return values
are 0 for a numeric DEF FN function, and the null string ("") for a
string DEF FN function.
■ DEF FN must define a function before the function is used. If you
call the function before it is defined by DEF FN, Visual Basic generates
the error message, "Function not defined." DEF FN function definitions
cannot appear inside other DEF FN definitions. In addition, functions
defined by DEF FN cannot be recursive.
■ Functions defined by DEF FN can be used only in the module in which
they are defined. The FUNCTION statement provides a better way of
defining functions.
■ A DEF FN-defined function can share variables with the module-level
code. Variables not in parameterlist are global - that is, their values
are shared with the module-level code. 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. If you assign a numeric value to a
string function name or assign a string value to a numeric function
name, Visual Basic generates 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 Visual 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
unpredictable results:
DEF FNShort
I=10
FNShort=1
END DEF
I=1 : PRINT FNShort + I + I
■ If Visual Basic reorders the expression so FNShort is called after
calculating (I+I), the result is 3 rather than 21. You usually can
avoid this problem by isolating the DEF FN function call:
I = 1 : X = FNShort : PRINT X + I + I
Note: Embedding I/O operations in DEF FN-defined functions used in
I/O statements, or embedding graphics operations in DEF FN-defined
functions in graphics statements, may cause similar problems.