Returning Complex Type Data

If a Fortran program expects a procedure to return a complex or double-complex  value, the Fortran compiler adds an additional argument to the beginning of the called procedure argument list. This additional argument is a pointer to the location where the called procedure must store its result.

Example below shows the Fortran code for returning a complex data type procedure called wbat and the corresponding C routine.

Example of Returning Complex Data Types from C to Fortran

Fortran code
complex bat, wbat
real x, y  
bat = wbat ( x, y )

Corresponding C Routine
struct _mycomplex { float real, imag };
typedef struct _mycomplex _single_complex;
void wbat_ (_single_complex location, float *x, float *y)

{
float realpart;
float imaginarypart;
... program text, producing realpart and
imaginarypart...
*location.real = realpart;
*location.imag = imaginarypart;
}

In the above example, the following restrictions and behaviors apply:

If the function returned a double complex value, the type float would be replaced by the type double in the definition of location in wbat.