13.2 Declaring Subprograms and Parameters
You declare a subprogram by naming it in an EXTERNAL
statement in the calling program. You may also declare the
data type of each parameter. If the subprogram is a function,
the EXTERNAL statement also lets you specify the data type
of the returned value.
The following statements are subprogram declarations using
the EXTERNAL statement:
EXTERNAL SUB my_sub (LONG, STRING)
EXTERNAL GFLOAT FUNCTION my_func (GFLOAT, LONG, GFLOAT)
EXTERNAL REAL FUNCTION determinant (LONG DIM(,))
Note that the parameter lists contain only data type and di-
mension information; they cannot contain any format or
actual parameters. When the external procedure is in-
voked, BASIC ensures that the actual parameter data type
matches the data type specified in the EXTERNAL declara-
tion. However, BASIC does not check to make sure that the
parameters declared in the EXTERNAL statement match
those in the external routine. You must ensure that these
parameters match.
You can pass data of any BASIC data type to a BASIC sub-
program, including RFAs and RECORDs. BASIC allows you
to pass up to 255 parameters, separated by commas. The
data can be any one of the following:
.
Constants
.
Variables
.
Expressions
.
Functions
.
Array elements
.
Entire arrays (but not virtual arrays)
For passing constants, variables, functions, and array ele-
ments, name them in the argument list. For example:
CALL SUB01(var1, var2)
CALL SUB02(Po_num%, Vouch, 66.67, Cust_list(5), FNA(B%))
However, when passing an entire array, you must use a
special format. You specify the array name followed by com-
mas enclosed in parentheses. The number of commas must
be the number of array dimensions minus one. For ex-
ample, array_name( ) is a one-dimensional array, array_
name(,) is a two-dimensional array, array_name(,,) is a
three-dimensional array, and so on.
The following example creates a three-dimensional array,
loads the array with values, and passes the array to a sub-
program as a parameter. The subprogram can access and
change values in array elements, and these changes remain
in effect when control returns to the main program.
EXAMPLE: Click to display example.
If you do not specify data types for parameters, the default
data type is determined by:
.
The last specified parameter data type
.
An OPTION statement
.
A BASIC compilation qualifier (for example, /REAL_
SIZE=DOUBLE)
.
The system default
The last specified parameter data type overrides all the other
default data types, the defaults specified in the OPTION
statement override any compilation qualifiers and system de-
faults, and so on. See Chapter 2 for more information about
the OPTION statement and establishing data-type defaults.
When you know the length of a string or the dimensions of
an array at compile time, you can achieve optimum perfor-
mance by passing them BY REF. When you call programs
written in other languages, the practice of declaring subpro-
grams and specifying the data types of parameters becomes
more important because other languages may not use the
BASIC default parameter-passing mechanisms. For more
information about calling subprograms written in other
languages, see Chapter 20.