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

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.