Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Fortran return statement obsolete?

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?

like image 277
rsaavedra Avatar asked Apr 12 '18 16:04

rsaavedra


People also ask

What does RETURN do in Fortran?

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.

What is subroutine in Fortran?

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

What is entry in Fortran?

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.


2 Answers

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
like image 121
Vladimir F Героям слава Avatar answered Oct 13 '22 18:10

Vladimir F Героям слава


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.

like image 33
francescalus Avatar answered Oct 13 '22 16:10

francescalus