The way that the return value of a function is handled is very much like a
simple assignment statement. The value is actually returned in one or two of
the registers of the CPU, depending on the size of the data type. Consequently
there is no problem in handling the value of any function that returns a
numerical value as long as the storage used by the value being returned and the
value expected correspond
(see Table ).
The case of a function that returns a character string is more complex. The way that Sun FORTRAN returns a character variable as a function value is to add two hidden extra entries to the beginning of the argument list. These are a pointer to a character variable and the value of the length of this variable. If a C function wishes to emulate a FORTRAN CHARACTER function, then you must explicitly add these two extra arguments to the C function. Any value that the C function returns will be ignored. Here is an example to illustrate this.
PROGRAM CFUNC CHARACTER*(10) VAR, FUNC VAR = FUNC( 6 ) PRINT *, VAR ENDC function:
void func_( char *retval, int length, int *n ) { char *cp; int i, max; /* Find the number of characters to be copied. */ if( *n < length ) max = *n; else max = length; /* Set a local character pointer equal to the return address. */ cp = retval; /* Copy some asterisks to the "return value". */ for( i = 0 ; i < max ; i++ ) *cp++ = '*'; /* Fill the rest of the string with blanks. */ for( ; i < length ; i++ ) *cp++ = ' '; }
The C function copies some asterisks into the location that Sun FORTRAN will interpret as the return value of the FORTRAN CHARACTER function. The number of such asterisks is specified by the single argument of the FORTRAN function and the rest of the string is filled with blanks.
CNF and F77 Mixed Language Programming -- FORTRAN and C