DEF FN Statement ---------------------------------------------------------------------------- Action Defines and names a function. Syntax 1 DEF FNname-(parameterlist)- = expression Syntax 2 DEF FNname-(parameterlist)- - statementblock- FNname = expression - statementblock- - EXIT DEF- - statementblock- END DEF Remarks The DEF FN statement uses the following arguments. ----------------------------------------------------------------------------- Argument Description ---------------------------------------------------------------------------- name A legal variable name, which is always prefixed with FN (for example. FNShort). The name (including the FN prefix) can be up to 40 characters long and 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. Argument Description ---------------------------------------------------------------------------- FNString$ FNString% FNString# To return a value from a function defined by DEF FN, 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. Functions defined by DEF FN do Argument Description ---------------------------------------------------------------------------- Functions defined by DEF FN do not accept arrays, records, or fixed-length strings as arguments. expression In both versions of syntax, 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 0 for a numeric DEF FN function, and the null string ("") for a string DEF FN function. The argument parameterlist has the following syntax. variable - AS type- -, variable - AS type--... ----------------------------------------------------------------------------- Argument Description ---------------------------------------------------------------------------- variable Any valid BASIC variable name. AS type The argument type is INTEGER, LONG, SINGLE, DOUBLE, CURRENCY, or STRING. You also can 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 statement. For more information, see the FUNCTION entry in this book and Chapter 2, "SUB and FUNCTION Procedures" in the Programmer's Guide. DEF FN must define a function before the function is used. If you call a DEF FN function before it is defined, 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. The EXIT DEF statement causes an immediate exit from the executing DEF FN function. Program execution continues where the DEF FN function was invoked. DEF FN functions can be used only in the module in which they are defined. A function defined by DEF FN can share variables with the module-level code. Variables not in parameterlist are global--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, BASIC generates the error message Type mismatch. If the function is numeric, DEF FN name returns a value with the precision specified by name. For example, if name specifies a double-precision variable, then the value returned by DEF FN name is double precision, regardless of the precision of expression. Because BASIC may rearrange arithmetic expressions for greater efficiency, avoid using functions defined by DEF FN 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 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 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. See Also FUNCTION, STATIC Example The following program uses a function defined by DEF FN to calculate the factorial of a number (for example, the factorial of 3 is 3 times 2 times 1). DEF FNFactorial# (X%) STATIC Tmp#, I% Tmp# = 1 FOR I% = 2 TO X% Tmp# = Tmp# * I% NEXT I% FNFactorial# = Tmp# END DEF INPUT "Enter an integer. ", Num% PRINT . PRINT Num%; "factorial is"; FNFactorial#(Num%)