5

Is a plain RETURN statement (without any arguments) just before END SUBROUTINE in large legacy codes a good practice or should we just remove it?

The code I am working with is a big legacy code (for scientific computing) where parts of the code were originally written with Fortran 77, while majority new development is in Fortran 95 and above. There is some interspersed C code an Python scripting as well.

The Intel developer guide clearly mentions that End Subroutine already initiates Return: https://software.intel.com/en-us/fortran-compiler-developer-guide-and-reference-end

On the other hand, I remember having been taught in 2000s that it is always good practice to specify an explicit RETURN statement before END SUBROUTINE.

Typical subroutine structure is mentioned as follows (e.g., Chapman's Fortran for Scientists and Engineers, 4th Edition):

SUBROUTINE subroutine_name ( argument_list )
...
(Declaration section)
...
(Execution section)
...
RETURN
END SUBROUTINE [subroutine_name]

If Return statement (without any arguments) is not a good practice then why even mention it as part of the structure, especially outside the "Execution section"? CYCLE or EXIT etc. are never mentioned as part of a standard Subroutine structure. So, why the RETURN?

user84428
  • 151
  • 4
  • 1
    I never consider any redundant syntax as good practice, but as this is legacy code I wouldn't change anything I didn't have to. – user207421 Nov 06 '19 at 07:33
  • Intel broke the documentation link so here is a version that works as of the timestamp on this comment: https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2023-2/end.html – Jeff Hammond Aug 16 '23 at 07:41

2 Answers2

4

As the Intel guide mentions, it's pointless since the "end subroutine" statement already handles it. Return is useful if you need to, say, return inside a loop or such (but then there's a school of thought saying you should structure your control flow such that you never return in the middle of a loop. YMMV).

janneb
  • 36,249
  • 2
  • 81
  • 97
  • 2
    Might be worth mentioning that it depends on the language revision being targeted (which is why you see this in legacy code). RETURN (and STOP, equivalently) were required in Fortran 66 and earlier. – IanH Nov 06 '19 at 11:54
2

It is a noise. A useless statement that only distracts from the useful code. Inexperienced people may wonder why it is there.

It is similar to putting a STOP right before the END PROGRAM. That one is even worse, because it has some side-effects that can confuse inexperienced programmers and users of the program.

Some very old Fortran version (the first 66 standard) may have required it, but that is computing prehistory.

  • 1
    Not quite correct. Fortran 66, 8.3.1 Defining Function Subprograms ... (6) The function subprogram must contain at least one `RETURN` statement. 8.4.1 Defining Subroutine Subprograms ... (5) The subroutine subprogram must contain at least one `RETURN` statement. – steve Nov 06 '19 at 17:20
  • Note F77 removed the requirements from F66. 15.8.2 Execution of a RETURN Statement ... Execution of an END statement in a function or subroutine subprogram has the same effect as executing a RETURN statement in the subprogram. – steve Nov 06 '19 at 17:26
  • Should have checked the document. I recalled there was something like that, but missed the exact date. – Vladimir F Героям слава Nov 06 '19 at 17:32
  • Now I see IanH even mentioned it in a comment. – Vladimir F Героям слава Nov 06 '19 at 17:47