11.2.2 Multiline DEF Functions
The DEF statement can also define multiline functions.
Multiline DEF functions are useful for expressing com-
plicated functions. Note that multiline DEF functions do
not have the equal sign and defining expression on the first
line. Instead, this expression appears in the function block,
assigned to the function name.
Multiline function definitions can contain any constant, vari-
able, BASIC built-in function, or user-defined function. In
BASIC, the function definition can contain a reference to the
function you are defining. Therefore, a multiline DEF func-
tion can be recursive, or invoke itself. However, BASIC does
not detect infinitely recursive DEF functions during compi-
lation. If your program invokes an infinitely recursive DEF
function, BASIC will eventually signal a fatal run-time error,
typically the error, ``Access violation''.
You can use either the END DEF or EXIT DEF statements
to exit from a user-defined function. The EXIT DEF state-
ment is equivalent to an unconditional transfer to the END
DEF statement.
The following example shows a multiline DEF function that
uses both the EXIT and END DEF statements. The defining
expression of the function is in the ELSE clause. This as-
signs a value to the function if A is less than 10. The second
set of output shows what happens when A is greater than 10;
BASIC prints ``OUT OF RANGE'' and executes the EXIT
DEF statement. The function returns zero because con-
trol is transferred to the END DEF statement before a value
was assigned. In this way, this example tests the arguments
before the function is evaluated.
DEF fn_discount(A)
IF A > 10
THEN
PRINT "OUT OF RANGE"
EXIT DEF
ELSE
fn_discount = A^A
END IF
END DEF
INPUT Z
PRINT fn_discount(Z)
END
Output 1
? 4
256
Output 2
? 12
OUT OF RANGE
0
If you do not explicitly declare the function with the
DECLARE statement, the restrictions for naming a mul-
tiline DEF function are the same as those for the single-line
DEF function. However, explicitly declaring a DEF function
can make a program easier to read and understand. For in-
stance, Example 1 concatenates two strings and Example 2
returns a number in a specified modulus.
EXAMPLE: Click to display example.
EXAMPLE: Click to display example.
Output
PLEASE INPUT THE VALUE AND THE MODULUS? 7, 5
2
Because these functions are declared in DECLARE state-
ments, the function names do not have to conform to the
traditional BASIC rules for naming functions.
Recursion occurs when a function calls itself. The following
example defines a recursive function that returns a number's
factorial value:
DECLARE INTEGER FUNCTION factor ( INTEGER )
DEF INTEGER factor ( INTEGER F )
IF F <= 0%
THEN factor = 1%
ELSE factor = factor(F - 1%) * F
END IF
END DEF
INPUT "INPUT N TO FIND N FACTORIAL"; N%
PRINT "N! IS"; factor(N%)
END
Output
INPUT N TO FIND N FACTORIAL? 5
N! IS 120
Any variable accessed or declared in the DEF function and
not in the formal parameter list is global to the program
unit. When BASIC evaluates the user-defined function, these
global variables contain the values last assigned to them in
the surrounding program module.
To prevent confusion, variables declared in the formal pa-
rameter list should not appear elsewhere in the program.
Note that if your function definition actually uses global vari-
ables, these variables cannot appear in the formal parameter
list.
You cannot transfer control into a multiline DEF function
except by invoking it. You should not transfer control out of a
DEF function except by way of an EXIT DEF or END DEF
statement. This means that:
.
If the DEF function contains an ON ERROR GOTO,
GOTO, ON GOTO, GOSUB, ON GOSUB, or RESUME
statement, that statement's target line number must also
be in that DEF function.
.
An ON ERROR GO BACK statement can transfer control
out of a DEF function; however, a RESUME statement
in an error handler outside the DEF function cannot
transfer control back into the DEF function.
.
If the DEF function contains a handler, and was invoked
from a protected region, an EXIT HANDLER statement
causes control to be transferred to the specified handler
for that protected region. However, if the DEF function
contains a handler but was not invoked from a protected
region, an EXIT HANDLER statement causes control to
be transferred to the default error handler.
.
A subroutine cannot be shared by more than one DEF
function. However, if you rewrite the subroutine as a
DEF function with no parameters, other function defini-
tions can share it.
A DEF function never changes the value of a parameter
passed to it. Also, because formal parameters are local to
the function definition, you cannot access the values of these
variables from outside the DEF statement. These variable
names are known only inside the DEF statement.
In the following example, the variable first is declared only in
the function fn_sum . When BASIC sees the second PRINT
statement, it assumes that first is a new variable that is
not declared in the main program. If you try to run this
example, BASIC signals the error ``Explicit declaration of
first required''. If you do not specify the OPTION TYPE =
EXPLICIT statement, BASIC assumes that first is a new
variable and initializes it to zero.
OPTION TYPE = EXPLICIT
DECLARE INTEGER A, B
DEF fn_sum(INTEGER first, INTEGER second) = first + second
A = 50
B = 25
PRINT fn_sum(A, B)
PRINT first
END