HP Fortran Programmer Guide (766160-001, March 2014)
directive. 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 %VAL and %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$ 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; see “Case sensitivity” (page 114), which includes an example program that uses the
$HP$ ALIAS directive and the%VAL and %REFbuilt-in functions to call a C function. For other
examples, see “Complex numbers” (page 112)and “File handling” (page 120). Note that the example
Fortran program in “Arrays” (page 116)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 and %REF built-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$ ALIAS directive 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 +uppercase option. Therefore, we recommend using the $HP$ ALIAS
directive. This directive enables you to associate an external name with an external name, even if
the external name uses uppercase characters.
114 Calling C routines from HP Fortran