HP Fortran Programmer's Guide (B3908-90031; September 2011)

Calling C routines from HP Fortran
Argument-passing conventions
Chapter 8188
Argument-passing conventions
The important difference between the argument-passing conventions of HP C and HP Fortran 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 provides two built-in functions, %VAL and %REF, to override Fortrans 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;
“Case sensitivity” on page 189, 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”
on page 185 and “File handling” on page 199. Note that the example Fortran program in “Arrays” on
page 192 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 Programmer's Reference.