11.2.1 Single-Line DEF Functions
In a single-line DEF, the function name, the formal pa-
rameter list, and the defining expression all appear on the
same line. The defining expression specifies the calculations
that the function performs. You can pass up to 255 argu-
ments to this function through the formal parameter list.
These parameters are variables local to the function defini-
tion, and each formal parameter can be preceded by a data
type keyword.
The following example creates a function named fnra-
tio . This function has two formal parameters: numer and
denomin , whose ratio is returned as a REAL value.
When the function is invoked, BASIC does the following:
The PRINT statement then prints the returned value.
DEF REAL fnratio (numer, denomin) = numer / denomin
PRINT fnratio(5.6, 7.8)
END
Output
.717949
Note that the actual parameters you supply must agree in
number and data type with those in the formal parameter
list; you must supply numeric values for numeric variables,
and string values for string variables.
The defining expression for a single-line function definition
can contain any constant, variable, BASIC built-in func-
tion, or any user-defined function except the function being
defined. The following examples are valid function definitions:
DEF FN_A(X) = X^2 + 3 * X + 4
DEF FN_B(X) = FN_A(X) / 2 + FN_A(X)
DEF FN_C(X) = SQR(X+4) + 1
DEF CUBE(X) = X ^ 3
Note that the name of the last function defined does not begin
with FN. This is valid as long as no reference to the function
lexically precedes the function definition.
You can also define a function that has no formal param-
eters. The following function definition uses three BASIC
built-in functions to return an integer corresponding to the
day of the month:
.
DATE$( 0 ) returns a date string in the form dd-Mmm-
yy .
.
The SEG$ function strips out of this value the characters
starting at character position 1 up to and including the
character at position 2 (the day number).
.
The VAL% function converts this resulting numeric
string to an integer. In this way, fnday_number returns
the day of the month as an integer.
DEF INTEGER fnday_number = VAL% (SEG$(DATE$(0%), 1%, 2%))