1

I found that gfortran allows return statement in the main program of fortran, but ifort does not allow this, giving compiling errors.

I do not understand the reason why ifort does not like the return statement in the main program, considering that return is useful when I want to finish the execution before the program reaching the last line of code. Does ifort recommend better ways to early terminate the main program. Is it stop?

Is this gfortran behavior a non-standard extension, which extends the use of return from procedures to the main program?

Youjun Hu
  • 991
  • 6
  • 18

2 Answers2

3

You have answered your own question - this is a non-standard extension:

ian@eris:~/work/stack$ cat ret.f90
Program ret
  Implicit None
  Return
End Program ret
ian@eris:~/work/stack$ gfortran-10 --version
GNU Fortran (GCC) 10.0.1 20200225 (experimental)
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

ian@eris:~/work/stack$ gfortran-10 ret.f90
ian@eris:~/work/stack$ gfortran-10 -Wall -Wextra -fcheck=all -std=f2008 ret.f90 
ret.f90:3:8:

    3 |   Return
      |        1
Error: GNU Extension: RETURN statement in main program at (1)

If you want portability and standard conformance (and all should) I strongly recommend always using the -std flag on gfortran, and its equivalent with other compilers.

As you say, stop is the way to exit a main program.

Ian Bush
  • 6,996
  • 1
  • 21
  • 27
2

A return statement is not permitted inside a main program (except within an internal subprogram). This can be seen by looking at the syntax rules for Fortran (Fortran 2018, 15.6.2.7):

R1542 return-stmt is RETURN [ scalar-int-expr ]

C1575 (R1542) The return-stmt shall be in the inclusive scope of a function or subroutine subprogram.

The constraint C1575 gives precisely the prohibition ifort uses. Being a numbered constraint any Fortran compiler must be able to detect and report such a violation by the programmer, but the compiler may choose to allow it (in some undefined way). Ian Bush shows how gfortran can be asked to complain about such invalid use of return.

And to repeat: yes, stop (and error stop) "return" from the main program (back to the operating system) by terminating the program's execution. (A non-standard exit function/subroutine may also be used in some settings, but I'd suggest you avoid that in the same way as avoiding return in a main program.)

francescalus
  • 30,576
  • 16
  • 61
  • 96
  • Good point about error stop - I had forgotten about that – Ian Bush Jan 20 '21 at 11:19
  • `stop` also terminates any threads running in parallel with the main program thread, in case you use OpenMP. Of course such threads could also have a way to `return` (e.g. if a flag variable is set), but in some cases it's just convenient to use `stop` knowing this will terminate all threads. – Pap Jan 21 '21 at 06:27