HP Fortran Programmer Guide HP-UX 11i v1, HP-UX 11i v2, and HP-UX 11i v3 (B3908-90032,December 2012)

ALIASdirective. The %REFfunction tells Fortran that its argument is to be passed by reference (as
when passing an array or pointer in C), and the %VALfunction 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 would be declared as follows:
INTEGER :: ptr, i
INTEGER, DIMENSION(100) :: iarray
The call from Fortran to the C function (using the %VALand %REFbuilt-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 %VALand %REFbuilt-in functions. Using
the $HP$ ALIASdirective 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 %REFat each call site.
Here is the $HP$ ALIASdirective 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$ ALIASdirective to handle case-sensitivity difference between C and
HP Fortran; “Case sensitivity” on page 189, which includes an example program that uses the
$HP$ ALIASdirective and the%VAL and %REFbuilt-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$ ALIASdirective and the and %REFbuilt-in functions, see
the HP Fortran Programmer's Reference.
Case sensitivity
Unlike HP Fortran, C is a case-sensitive language. HP Fortran converts all external names to
lowercase, and it disregards the case of internal names. Thus, for example, the names fooand
FOOare the same in Fortran. C, however, is a case-sensitive language: fooand FOOare different
in C. If an HP Fortran program is linked to a C object file and references a C function that uses
uppercase characters in its name, the linker will not be able to resolve the reference.
If case sensitivity is an issue when calling a C function from an HP Fortran program, you have two
choices:
Compile the Fortran program with the +uppercaseoption, which forces Fortran to use
uppercase for external names.
Use the $HP$ ALIASdirective to specify the case that Fortran should use when calling an
external name.
It is unusual that all names in the C source file would be uppercase, which would be the only case
justifying the use of the +uppercaseoption. Therefore, we recommend using the $HP$
ALIASdirective. This directive enables you to associate an external name with an external name,
even if the external name uses uppercase characters.
112 Calling C routines from HP Fortran