The example below demonstrates a C main module calling the FORTRAN function fact. This function returns the factorial of an integer value.
/* C source file - calls FORTRAN function.
* Compile in medium or large model.
*/
int __fortran fact( int N );
/* FORTRAN keyword causes C to use FORTRAN/Pascal
* calling and naming conventions.
* Integer parameter passed by value.
*/
main()
{
int x = 3;
int y = 4;
printf( “The factorial of x is %4d”, fact( x ) );
printf( “The factorial of y is %4d”, fact( y ) );
printf( “The factorial of x+y is %4d”, fact( x + y ) );
}
C FORTRAN source file - factorial function
C
$NOTRUNCATE
INTEGER*2 FUNCTION FACT (N)
INTEGER*2 N [VALUE]
C
C N is received by value, because of VALUE attribute
C
INTEGER*2 I
FACT = 1
DO 100 I = 1, N
FACT = FACT * I
100 CONTINUE
RETURN
END
In the example above, the C program adopts the naming convention and calling convention of the FORTRAN subroutine. Both programs must agree on whether parameters are passed by reference or by value. Note that the C program passes the parameters by value rather than by reference. Passing parameters by value is the default for C. To accept parameters passed by value, the keyword VALUE is used in the declaration of N in the FORTRAN function. The __fortran keyword directs C to call fact with the FORTRAN/Pascal naming convention (as FACT); __fortran also directs C to call fact with the FORTRAN/Pascal calling convention.
When passing a parameter that should not be changed, pass the parameter by value. Passing by value is the default method in C and is specified in FORTRAN by applying the VALUE attribute to the parameter declaration.