A Fortran function that returns a value of type BYTE , INTEGER, REAL, LOGICAL, DOUBLE PRECISION, or REAL*16 is equivalent to a C function that returns a compatible type (see Table 11–1). There are two extra arguments for the return values of character functions, and one extra argument for the return values of complex functions.
The following example returns a REAL or float value. BYTE, INTEGER, LOGICAL, DOUBLE PRECISION, and REAL*16 are treated in a similar way:
Table 11–12 Functions Returning a REAL or Float Value
Fortran calls C |
C calls Fortran |
||
---|---|---|---|
|
|
The situation for interoperability of COMPLEX data differs between 32-bit implementations and 64-bit SPARC implementations.
A Fortran function returning COMPLEX or DOUBLE COMPLEX on 32-bit platforms is equivalent to a C function with an additional first argument that points to the return value in memory. The general pattern for the Fortran function and its corresponding C function is:
Fortran function |
C function |
||
---|---|---|---|
|
|
Table 11–13 Function Returning COMPLEX Data (32–bit SPARC)
Fortran calls C |
C calls Fortran |
||
---|---|---|---|
|
|
In 64-bit SPARC environments, COMPLEX values are returned in floating-point registers: COMPLEX and DOUBLE COMPLEX in %f0 and %f1, and COMPLEX*32 in %f0, %f1, %f2, and %f3. For 64–bit SPARC, a C function returning a structure whose fields are all floating-point types will return the structure in the floating-point registers if at most 4 such registers are needed to do so.The general pattern for the Fortran function and its corresponding C function on 64–bit SPARC platforms is:
Fortran function |
C function |
---|---|
COMPLEX FUNCTION CF(a1, a2, ..., an) |
struct {float r,i;} cf_ (a1, a2, ..., an) |
Table 11–14 Function Returning COMPLEX Data (64–bit SPARC)
Fortran calls C |
|
---|---|
|
|
C calls Fortran |
|
|
Passing strings between C and Fortran routines is not encouraged. However, a Fortran character-string-valued function is equivalent to a C function with two additional first arguments—data address and string length. The general pattern for the Fortran function and its corresponding C function is:
Fortran function |
C function |
|
---|---|---|
CHARACTER*n FUNCTION C(a1, ..., an) |
|
Here is an example:
Table 11–15 A Function Returning a CHARACTER String
Fortran calls C |
C calls Fortran |
||
---|---|---|---|
|
|
In this example, the C function and calling C routine must accommodate two initial extra arguments (a pointer to the result string and the length of the string) and one additional argument at the end of the list (length of character argument). Note that in the Fortran routine called from C, it is necessary to explicitly add a final null character. Fortran strings are not null-terminated by default.