Sample program: C/C++ calling Fortran

The following example illustrates how program units written in different languages can be combined to create a single program. It also demonstrates parameter passing between C/C++ and Fortran subroutines with different data types as arguments.

#include <stdio.h>
extern double add(int *, double [], int *, double []);

double ar1[4]={1.0, 2.0, 3.0, 4.0};
double ar2[4]={5.0, 6.0, 7.0, 8.0}; 

main()
{
int x, y; 
double z;

x = 3;
y = 3; 


z = add(&x, ar1, &y, ar2); /* Call Fortran add routine */ 
/* Note: Fortran indexes arrays 1..n */
/* C indexes arrays 0..(n-1) */ 

printf("The sum of %1.0f and %1.0f is %2.0f \n",
ar1[x-1], ar2[y-1], z);
}

The Fortran subroutine is:

C Fortran function add.f - for C/C++ interlanguage call example

C Compile separately, then link to C/C++ program 

REAL*8 FUNCTION ADD (A, B, C, D) 
REAL*8 B,D
INTEGER*4 A,C
DIMENSION B(4), D(4)
ADD = B(A) + D(C)
RETURN
END