%REF and %VAL Intrinsic Functions

Intel® Fortran provides two additional intrinsic functions, %REF and %VAL, that can be used to specify how actual arguments are to be passed in a procedure call. They should not be used in references to other Fortran procedures, but may be required when referencing a procedure written in another programming language such as C.

%REF(X)

Specifies that the actual argument X is to be passed as a reference to its value. This is how Intel Fortran normally passes arguments except those of type character. For each character value that is passed as an actual argument, Intel Fortran normally passes both the address of the argument and its length (with the length being appended on to the end of the actual argument list as a hidden argument. Passing a character argument using %REF does not pass the hidden length argument.

%VAL(X)

Specifies that the value of the actual argument X is to be passed to the called procedure rather than the traditional mechanism employed by Fortran where the address of the argument is passed.

In general, %VAL passes its argument as a 32-bit, sign extended, value with the following exceptions: the argument cannot be an array, a procedure name, a multi-byte Hollerith constant, or a character variable (unless its size is explicitly declared to be 1).

In addition, the following conditions apply:

This behavior is compatible with the normal argument passing mechanism of the C programming language, and it is to pass a Fortran argument to a procedure written in C where %VAL is typically used.

The intrinsic procedures %REF and %VAL can only be used in each explicit interface block, or in the actual CALL statement or function reference as shown in the example that follows.

Calling Intrinsic Procedures

PROGRAM FOOBAR
  INTERFACE
     SUBROUTINE FRED(%VAL(X))
        INTEGER :: X
     END SUBROUTINE FRED
     FUNCTION FOO(%REF(IP))
        INTEGER :: IP, FOO
     END FUNCTION FOO
  END INTERFACE
  ...
  CALL FRED(I)  ! The value of I is passed to FRED
  J = FOO(I)    ! I passed to FOO by reference,
  ! FOO receives a reference to
  ! the value of I.
END PROGRAM

Alternatively:
PROGRAM FOOBAR
 INTEGER :: FOO
 EXTERNAL FOO, FRED
 CALL fred(%VAL(I))
 J = FOO(%REF(I))
END PROGRAM