Fortran 95 programs should use the VALUE attribute in dummy arguments when being called from C, and supply an INTERFACE block for C routines that are called from Fortran 95.
Table 11–11 Passing Simple Data Elements Between C and Fortran 95
Fortran 95 calls C |
C calls Fortran 95 |
||
---|---|---|---|
|
|
Note that if the C routine will be called with different data types as an actual argument, you should include a !$PRAGMA IGNORE_TKR I in the interface block to inhibit the compiler from requiring a match in type, kind, and rank between the actual and dummy argument.
With legacy Fortran 77, call by value was available only for simple data, and only by Fortran 77 routines calling C routines. There was no way for a C routine to call a Fortran 77 routine and pass arguments by value. Arrays, character strings, or structures are best passed by reference.
To pass a value to a C routine from a Fortran 77 routine, use the nonstandard Fortran function %VAL(arg) as an argument in the call.
In the following example, the Fortran 77 routine passes x by value and y by reference. The C routine incremented both x and y, but only y is changed.
Fortran calls C |
|||
---|---|---|---|
Fortran routine:
C routine:
Compiling and running produces output:
|