HP Fortran Programmer's Reference (September 2007)

Program units and procedures
Arguments
Chapter 7 179
•%VAL(
arg
) specifies that the value of
arg
—rather than its address—is to be passed to the
referenced procedure.
arg
can be a constant variable, an array element, or a derived-type
component.
%REF(
arg
) specifies that the address of
arg
is to be passed to the referenced procedure.
Because this is how HP Fortran normally passes all noncharacter arguments, %REF is
useful only when
arg
is of type character. The effect of using %REF with a character
argument is to suppress the hidden length parameter.
These built-in functions are typically used to pass arguments from Fortran to a procedure
written in another language, such as a C function. The following example illustrates this use.
The program consists of a Fortran 90 main program unit and a C function. The main program
calls the C function, passing 4 arguments: an integer constant, a real variable, a character
variable, and an integer expression. The main program uses the built-in functions to change
Fortran’s argument-passing conventions to conform to C. C expects all arguments except the
string—Fortran’s character variable—to be passed by value. It expects the string to be passed
by reference, without the hidden length parameter.
Example 7-6 pass_args.f90
PROGRAM main
REAL :: x = 3.4
INTEGER :: i1 = 5, i2 = 7
! C expects strings to be null-terminated, so use the
! concatenation operator to append a null character.
CHARACTER(LEN=5) :: str = "Hi!"//CHAR(0)
! Pass 4 arguments--a constant, a variable, a character
! variable, and an expression--to a function written in C.
! Use HP Fortran’s built-in functions to change the
! argument-passing conventions to conform to C.
CALL get_args(%VAL(20), %VAL(x), %REF(str), %VAL(i1+i2))
END PROGRAM main
Example 7-7 get_args.c
#include <stdio.h>
/* accept 4 arguments from a Fortran 90 program, which are
* passed as C expects them to be passed
*/
void get_args(int i1, float x, char *s, int i2)
{
/* display argument values */
printf("First argument: %i\n", i1);
printf("Second argument: %f\n", x);
printf("Third argument: %s\n", s);
printf("Fourth argument: %i\n", i2);
}