z/OS Language Environment Writing Interlanguage Communication Applications
Previous topic | Next topic | Contents | Contact z/OS | Library | PDF


Passing an alternate return code from Fortran to C++

z/OS Language Environment Writing Interlanguage Communication Applications
SA38-0684-00

You can pass an alternate return code to a C++ routine from a Fortran subroutine by specifying the called Fortran subroutine in the extern "FORTRAN" linkage specification. The Fortran subroutine produces an alternate return code when alternate returns are specified as dummy arguments on the SUBROUTINE statement.

In an all-Fortran application, the alternate returns provide a way to return to a point in the calling program other than to the point immediately following the CALL statement. The following example illustrates how a Fortran routine would call a Fortran subroutine to use an alternate return:
CALL FSUB (ARG1, *22, ARG2, *66)
In this example, *22 and *66 specify two labels (22 and 66) to which control can be passed rather than to the point following the CALL statement. The corresponding subroutine would be coded as follows:
SUBROUTINE FSUB (DARG1, DARG2, *, *)
When the FSUB subroutine executes the following RETURN statement, control would pass to the calling program at the second alternate return, at label 66.
RETURN 2

In C++, you can use the fortrc() function and extern "FORTRAN" linkage specification to get the alternate return code from the Fortran RETURN statement of the Fortran call immediately preceding it. You must not have any other C++ code between the Fortran routine call and fortrc(), otherwise the result is undefined.

In the following example, the C++ routine calls the subroutine FSUB, whose SUBROUTINE and RETURN statements are shown above. The fortrc() function returns an alternate return code of 2.
extern "FORTRAN" {void fsub (float, float);}
#includes <stdlib.h>
int rc:
⋮
fsub(1.0,2.0);
rc=fortrc();
⋮

You cannot pass return code values from a called C++ function to a calling Fortran routine.

Go to the previous page Go to the next page




Copyright IBM Corporation 1990, 2014