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?
Return (in Subroutines) A return statement in a subroutine instructs Fortran to terminate the subroutine and return to the main program at the point where it departed. Thus it works like a stop statement in the main program, halting the program prematurely before the final end statement.
A Fortran subroutine is a block of code that performs some operation on the input variables, and as a result of calling the subroutine, the input variables are modified. An expression containing a function call: ! func1 is a function defined elsewhere. !
An entry name can be specified in an EXTERNAL statement and used as an actual argument. It cannot be used as a dummy argument. Execution of an ENTRY subprogram (subroutine or function) begins with the first executable statement after the ENTRY statement. The ENTRY statement is a nonexecutable statement.
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
You just do
if (error) return
a lot of code
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With