4
votes

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?

2
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

2 Answers

3
votes

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).

1
votes

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.