Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fortran return statement

Tags:

return

fortran

I'm trying to get some code compiled under gfortran that compiles fine under g77. The problem seems to be from a return statement:

ffuncs.f:934.13:

  RETURN E
         1

Error: Alternate RETURN statement at (1) requires a SCALAR-INTEGER return specifier

In the code anything E was specified as real*8:

IMPLICIT REAL*8 ( A - H , O -Z )

However, E was never given a value or anything in fact you never see it until the return statement. I know almost nothing about fortran. What is the meaning of a return statement with an argument in fortran?

Thanks.

like image 770
Brandon Avatar asked Aug 11 '26 08:08

Brandon


1 Answers

In FORTRAN (up to Fortran 77, which I'm very familiar with), RETURN n is not used to return a function value; instead, it does something like what in other languages would be handled by an exception: An exit to a code location other than the normal one.

You'd normally call such a SUBROUTINE or FUNCTION with labels as arguments, e.g.

  CALL MYSUB(A, B, C, *998, *999)
...
998 STOP 'Error 1'
998 STOP 'Error 2'

and if things go wrong in MYSUB then you do RETURN 1 or RETURN 2 (rather than the normal RETURN) and you'd be hopping straight to label 998 or 999 in the calling routine.

That's why normally you want an integer on that RETURN - it's not a value but an index to which error exit you want to take.

RETURN E sounds wrong to me. Unless there's a syntax I'm unaware of, the previous compiler should have flagged that as an error.

like image 154
Carl Smotricz Avatar answered Aug 14 '26 12:08

Carl Smotricz