Q(uick)BASIC Statement: DEF FN
Quick View
DEF FN
A procedure statement that defines and names a function
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- DEF FNname[(parameterlist)] = expression
- DEF FNname[(parameterlist)][statementblock]FNname = expression[statementblock][EXIT DEF][statementblock]END DEF
Description/Parameter(s)
parameterlist | One or more arguments in the following form: | |||||
|
||||||
expression | The return value of the function. | |||||
The FUNCTION statement provides a better way to define a function. |
See also:
Syntax
- DEF FNname[(parameterlist)] = expression
- DEF FNname[(parameterlist)][statements]FNname = expression[statements]END DEF
Description/Parameter(s)
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: |
|
|
To return a value from a DEF FN function, assign the value to the full function name: | |
|
|
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.
Example
DEFFN_EX.BAS is a program file in the subdirectory ADVR_EX that illustrates the DEF FN statement. To look at the program in the View window and, optionally, to run it, load the program using the File menu's Open Program command.
The program uses a DEF FN function to calculate the factorial of an input number (for example, the factorial of 3 is 3*2*1).
See also:
Syntax
- DEF FNname[(parameterlist)] = expression
- DEF FNname[(parameterlist)][statementblock]FNname = expression[statementblock][EXIT DEF][statementblock]END DEF
Description/Parameter(s)
- The name is a legal variable name, which is always prefixed with FN (for example: FNShort). The variable name (including the FNprefix) can be up to 40 characters long. 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 FNfunctions:
- FNString$
- FNString%
- FNString#
- To return a value defined by a DEF FNfunction, assign the value to the full function name:
- FNString$ = "No answer."
- parameterlist is a list of variable names, separated by commas. A parameterlist has the following syntax:
- variable [AS type] [,variable [AS type]]…
variable | Any valid BASIC variable name. |
AS type | 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. |
- 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.
- EXIT DEF immediately exits an executing DEF FNfunction. Program execution continues where the DEF FNfunction was invoked.
- 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 FNdo 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 FNfunction, and the null string ("") for a string DEF FNfunction.
Usage Notes
- DEF FNmust define a function before the function is used. If you call the function before it is defined by DEF FN, BASIC generates the error message, "Function not defined." DEF FNfunction definitions cannot appear inside other DEF FNdefinitions. In addition, functions defined by DEF FNcannot be recursive.
- Functions defined by DEF FNcan be used only in the module in which they are defined.
- A DEF FN-defined function 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 FNcan return either numeric or string values. DEF FNreturns 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 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 FNfunctions 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 FNfunction 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.
- Note: The ⮜ FUNCTION ⮞ statement offers a more straightforward way to define and name a new function.
Example
This example uses a DEF FN function to calculate the factorial of an integer (for example, the factorial of 3 is 3*2*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%)
See also: