Is Fortran return statement obsolete?
Asked Answered
S

3

10

I just want to know if the return statement in Fortran 2008 is obsolete, because it seems to be unnecessary to write it at the end of subroutines and functions.

Does it have some other utility?

Saw answered 12/4, 2018 at 16:47 Comment(0)
W
18

No, it is not obsolete.

It is used to exit a subroutine and a function. Whenever you want to exit in the middle of a subroutine, you use RETURN. For example, when some error happens or similar.

Using RETURN is an alternative to long IF conditions like:

if (no_error) then
     [...a lot of code...]
end if

Instead you just do:

 if (error) return
 [...a lot of code...]
Wyler answered 12/4, 2018 at 17:1 Comment(2)
I haven't considered that. I've seen many old codes that always have: lot of code... return end. And I thought return was something necessary to write, but found it is not.Saw
Over 40 years ago, some implementations of Fortran treated running into END as a STOP as if it were a main program, so I suppose the rule of implicit RETURN was adopted in F77 to eliminate a non-portability. I never ran into anyone who intended to rely on this.Glazed
K
4

As well as being able to complete execution of a function or subroutine at any point, rather than just before the end, the return statement may (currently) be used to give alternate return:

return 2

Alternate return is obsolete and soon to be deleted, but is something in Fortran 2008 that doesn't happen without return.

Kitsch answered 12/4, 2018 at 17:9 Comment(0)
S
-3

It is not obsolete but it seems optional, as I have seem programs compile and run without it. (Using ifort)

Sarawak answered 13/4, 2018 at 12:49 Comment(4)
I am not sure I understand. I have seen programs compile without almost any type of statement I can imagine. The shortest Fortran program is end so all statements are optional.Headspring
Optional to do what? I don't think this is a clear answer.Loralorain
I will assume that those downvotes ander some of my answers are just a coincidence. BTW I didn't vote for this question or answers under this question in any way.Headspring
Code that compiles and runs without a RETURN before END SUBROUTINE, certainly make the ending RETURN seem optional...Sarawak

© 2022 - 2024 — McMap. All rights reserved.