The important difference between the argument-passing conventions
of HP C and HP Fortran 90 is that Fortran
passes arguments by reference — that is, it passes the
address of the argument — whereas C passes non-array and
non-pointer arguments by value — that is, it passes a copy
of the argument. This difference affects calls not only to user-written
routines in C but also to all HP-UX system calls and subroutines,
which are accessed as C functions.
HP Fortran 90 provides two built-in functions,
%VAL and %REF,
to override Fortran's default argument-passing conventions
to conform to C. These functions are applied to the actual arguments
you want to pass, either in the argument list of the routine you
are calling or with the $HP$ ALIAS
directive. The %REF function tells
Fortran that its argument is to be passed by reference (as when
passing an array or pointer in C), and the %VAL
function tells Fortran that its argument is to be passed by value
(the default case in C).
Consider a C function having the following prototype declaration:
void foo(int *ptr, int iarray[100], int i); |
In Fortran, the actual arguments to be passed to foo
would be declared as follows:
INTEGER :: ptr, i INTEGER, DIMENSION(100) :: iarray |
The call from Fortran to the C function (using the %VAL
and %REF built-in functions) would
be as follows:
CALL foo(%REF(ptr), %REF(iarray), %VAL(i)) |
If the Fortran program were to make numerous calls to foo
at different call sites, you might find it more convenient to use
the $HP$ ALIAS
directive with the %VAL and %REF
built-in functions. Using the $HP$ ALIAS
directive allows you to establish the argument-passing modes for
each parameter in a particular routine once and for all, without
having to use %VAL and %REF
at each call site. Here is the $HP$ ALIAS
directive for the Fortran program that calls foo:
!$HP$ ALIAS foo(%REF, %REF, %VAL) |
Note that the functions are used here without arguments; their
positions in the argument list indicate the parameters to which
each applies.
You can also use the $HP$ ALIAS
directive to handle case-sensitivity difference between C and HP Fortran 90;
“Case sensitivity”, which includes
an example program that uses the $HP$ ALIAS
directive and the %VAL and %REF
built-in functions to call a C function. For other examples, see
“Complex numbers” and “File handling ”. Note that the
example Fortran program in “Arrays” does not require the built-in functions
because both Fortran and C pass arrays by reference.
For detailed information about the $HP$ ALIAS
directive and the %VAL and %REF
built-in functions, see the HP Fortran 90 Programmer's
Reference.