Question about "optional" final END in a fixed-format file of subroutines

Consider the fixed-format file routines.f:

      subroutine foo(n)
      implicit none
      integer n
      n = n + 1
      return
      end

      subroutine bar(n)
      implicit none
      integer n
      n = 2*n
      return
      end

      end

This compiles without errors or warnings with gfortran and ifort. If I remove the final end statement, it still compiles with no complaints. Is there a simple explanation for the role of the final end and why it is optional? Is the code “more correct” with or without the final end?

This issue came up in some Fortran code that is wrapped by SciPy; see MAINT Add an extra END to prini.f by hoodmane · Pull Request #15954 · scipy/scipy · GitHub. Apparently f2c requires the final end statement, so the pyodide project has to patch a Fortran file to get f2c to work.

Warren

1 Like

Yes, there is a simple explanation. The third end is the end of an unnamed main program. You have 2 subroutines and what is equivalent to program main ; end. “More correct” depends on what you’re trying to accomplish. f2c does not need the third to simply translate the subroutines to C. The linked scipy issue is rather amusing; given that none of the people seem to have the wherewithal to actually learn a bit of language that they’re trying to understand .

3 Likes

Thanks, kargl, I see what is happening now. So, over in the SciPy code, we definitely don’t want that extra end; that file should not be defining a MAIN function.

For the code posted, the third end is unneeded if all you want is to have f2c translate foo and bar.