20.3 Calling BASIC Subprograms from Other Languages
When you call a BASIC subprogram from another language,
there are some additional considerations that you should be
aware of. For example, although BASIC conforms to the
OpenVMS Calling Standard, you should specify explicit pass-
ing mechanisms when calling a routine written in another
language. The default passing mechanisms of BASIC may
not match what the procedure expects. In the following sec-
tion, FORTRAN refers to VAX FORTRAN and DEC Fortran.
FORTRAN passes and receives numeric data by reference;
only the default parameter-passing mechanisms are required
for passing numeric data back and forth between FORTRAN
and BASIC programs.
Both BASIC and FORTRAN pass strings by descriptor.
However, FORTRAN subprograms cannot change the length
of strings passed to them. Therefore, if you pass a string to a
FORTRAN subprogram, you must make sure that the string
is long enough to receive the result. You do this in one of two
ways:
.
Pre-extend the string. Set the string variable equal to
SPACE$ (n) , where n is large enough to receive the result.
.
Define the string as fixed-length. Name the string in a
COMMON or MAP statement.
Because the length of the returned string does not change, it
is either padded with spaces or truncated.
To pass an array to a FORTRAN subprogram, you must
specify BY REF.
Note that FORTRAN arrays are one-based, while BASIC ar-
rays are zero-based by default. For example, in FORTRAN
the array Two_D (5,3) represents a 5 by 3 matrix, while in
BASIC the array Two_d (5,3) represents a 6 by 4 matrix. You
can adjust your array bounds in BASIC by using the keyword
TO when defining the array bounds. For more information
about array bounds, see Chapter 7.
When passing two-dimensional arrays as parameters, keep
in mind that FORTRAN addresses array elements in col-
umn major order, while BASIC refers to array elements
in row major order. That is, FORTRAN arrays are of
the form Fortran_array (column,row), while BASIC ar-
ray elements are addressed as Basic_array (row,column).
The FORTRAN array Grid (x,y) is therefore referred to as
GRID (y,x) in BASIC. You should reverse references to ar-
ray elements when passing arrays between BASIC and
FORTRAN program modules. You can do this in one of two
ways:
.
Reverse array bounds in parameter lists
.
Switch row and column variables within loops in your
program module
Example 20-1 shows a BASIC program that passes a
two-dimensional array to a FORTRAN subprogram. The
FORTRAN subprogram is shown in Example 20-2.
You can pass only the data types that BASIC and FORTRAN
have in common. You cannot pass a complex number from
a FORTRAN program to a BASIC program, because BASIC
does not support complex numbers. However, you can pass
a complex number as two floating-point numbers and treat
them independently in the BASIC program.