Here is a second example, using some pass-by-reference parameters in C. This time, let’s calculate the hypotenuse of a triangle, using a function in Fortran to perform the actual calculation. First, let’s consider the Fortran side of the code. This time, we have just built a single subroutine (file = **pythagoras.f95**).

subroutine pythagoras(a, b, c) bind(C) use iso_c_binding implicit none real (C_FLOAT), intent(in) :: a, b real (C_FLOAT), intent(out) :: c c = sqrt(a*a + b*b) end subroutine pythagoras

From the perspective of writing the Fortran code, there is very little difference from a normal subroutine, except for the specification of the bind, use of the iso_c_binding module, and specification of the subroutine parameters as C types equivalents.

From the perspective of the C program (**hypotenuse.c**), the subroutine pythagoras is cited as being external, and when called, both pass-by-value and pass-by-reference parameters are tagged with &. This is because although there is a difference in C, in Fortran, all parameters are “pass-by-reference”.

#include <stdio.h> extern void pythagoras(float *a, float *b, float *c); int main(void) { float hyp, sideA, sideB; sideA = 3; sideB = 4; pythagoras(&sideA, &sideB, &hyp); printf("The hypotenuse of triangle with sides"); printf(" %.2f and %.2f is %.2f\n", sideA, sideB, hyp); return 0; }

The programs can then be compiled in the following manner:

gcc -c hypotenuse.c gfortran pythagoras.f95 hypotenuse.o

When the program (a.out) is executed, here is the output:

The hypotenuse of triangle with sides 3.00 and 4.00 is 5.00