SuperBASIC functions
and
procedures are defined with the DEFine FuNction and
DE~ine
PROCedure statements. A function
IS
activated (or called)
by
typing
its
name
at
the appropriate point
In
a SuperBASIC expression. The function must be included
,n
an
expression because
it
is
returning a value and the value must be used. A procedure
is
activated (or called)
by
typing
its
name
as
the tirst
Item
in
a SuperBASIC statement.
Data can be passed
Into
a funellon
or
procedure
by
appending a
list
ot
aelual parameters
after the function or procedure name.
ThiS
list
is
compared
to
a similar
list
appended
after the name of the function or procedure when
it
was
defined.
ThiS
second
list
is
called the formal parameters of the function or procedure. The formal parameters must
be SuperBASIC variables The actual parameters must be
an
array,
an
array slice or
a SuperBASIC
expression of which a
Single
variable or constant
is
the simplest form.
Since the actual parameters are actual expressions, they must have
an
actual type
associated with them. The formal parameters are merely used
to
indicate how the actual
parameters must be processed and
so
have no type associated with them. The items
in
each
list
of parameters are paired off
In
order when the function or procedure
IS
called
and the formal parameters become equivalent
to
the actual parameters. There are three
distinct ways of
uSing
parameters.
If
the actual parameter
is
a single vanabie and
If
data
IS
assigned
to
the formal parameter
In
the function or procedure then the data
is
also assigned
to
the corresponding actual
parameter
If
the actual parameter
is
an expression then assigning data
to
the corresponding formal
parameter will have no effect outside the procedure. Note that a variable can be turned
into an expression by enclosing
it
within brackets.
If
the actual parameter
is
a variable but has not previously been
set
then assigning data
to
the corresponding formal parameter
will
set
the variable speCified
as
the actual
parameter.
Variables can be defined
to
be local
to
a function or procedure with the LOCal statement
Local variables have no effect on similarly named variables outside the function or
procedure
in
which they are defined and
so
allow greater freedom
in
choosing sensible
variable names without the
risk
of
corrupting external
variables.
A local variable
IS
available
to
any inside function or procedure called from the procedure function
in
which
It
is
declared
to
be
local unless the function or procedure called contains a further local
declaration of the same vanable name.
Functions and procedures
in
SuperBASIC can be used recursively. That
is
a function
or procedure can call itself either directly or indirectly.
•
•
•
12/84
Command
DEFine FuNelion
DEFine PROCedure
RETurn
LOCal
Function
define a function
define a procedure
leave a function or procedure
(return data from a function)
define local data
In
a function or
procedure
functions
and
procedures
23