DEF FN
Define Function
This directive allows the user to name and define a numeric or string function. It can then be referred to at any point in the program by using the proper function to call up this definition. This allows an expression to be used repeatedly in a program without redefining it each time.
Syntax
Section titled “Syntax”DEF FNx (variable-list) = numeric-expression
DEF FNx$ (variable-list) = string-expressionx- is the function name. In Thoroughbred Basic release levels before 8.1B2, the function name must be a single, uppercase alphabetic character. Starting with release level 8.1B2, the function name must begin with an uppercase letter, can be up to 32 characters long, and can contain any combination of uppercase letters (A-Z), numbers (0-9), and the underscore character.
variable-list- is a list of variables to be used as arguments in resolving the numeric-expression or string-expression value.
numeric-expression- is any number.
string-expression- is any string.
Remarks
Section titled “Remarks”The principal use of this directive is to define the formulas, numeric expressions, or string expressions used throughout a program. This eliminates the need to use the full formula or expression each time a result is needed.
The variable-list may contain numeric variables and string variables, which are used in the defined numeric-expression or string-expression. Their type and position specify the type and position of the numeric expressions and string expressions to be used when the defined function is executed. The variable names used in the directive need not match the names used when the defined function is executed.
The defined function is invoked through the use of its given name. For example, if the programmer uses this directive as follows:
DEF FNA$(A,B,C) = STR(A) + STR(B) + STR(C)then the function could be used as follows:
LET THREE_NUMBER_STRING$ = FNA$(NUM1,500,DEC("AB"))The function defined can only be used within the program currently executing. It is not passed on to the next program that is RUN to a CALLed public program.
Values for variables named in the variable-list of this directive changes whenever the corresponding function is used. Therefore, the variable-list names should not be used for purposes other than this defined function to avoid unexpected results.
If this directive is used in Thoroughbred Basic Console Mode, an ERR=45 results.
Examples
Section titled “Examples”DEF FNQ (A,B,C,X) = (A * X**2) + (B * X) + Cdefines a calculation using 4 numeric variables, which enables you to use
LET A = FNQ(A,B,C,X)instead of
LET A = (A * X**2) + (B * X) + Ceach time the calculation must be coded.
DEF FNC (A$,B$) = (NUM (A$) + NUM (B$)) / 2is a numeric function, which accepts two string variables containing numeric data, and returns the mathematical average.
DEF FNDATE_TIME$ (CURRENT_TIME, CURRENT_DATE) = DAY + STR (INT (TIM) :"BB#0") + ":" + STR (INT (FPT (TIM)*60) :"00") + ":" + STR (INT (FPT (TIM*60) *60) :"00")defines a string expression which, when invoked by the function
FNDATE_TIME$ (TIM,DAY)returns today’s date and time in the format: MM/DD/YY HH:MI:SS. If the date and time were June 13, 1989 at noon, this returns:
"06/13/89 12:00:00"See also
Section titled “See also”FN Function
From the Thoroughbred Basic Language Reference, Volume I: A–E, version 8.9.1. Open this page in the PDF