Brandon
Brandon

Reputation: 137

Fortran return statement

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.

Upvotes: 3

Views: 7762

Answers (3)

M. S. B.
M. S. B.

Reputation: 29391

I think @Carl Smotricz has the answer. Does argument list of ffuncs has dummy arguments that are asterisks (to match the asterisk-label in the calls)? Or was this used without there being alternative returns? If there were no alternative returns, just delete the "E". If there are alternative returns, the big question is what the program was doing before at run time since the variable was of the wrong type and uninitialized. If the variable didn't have an integer value matching one of the expected branches, perhaps the program took the regular return branch -- but that's just a guess -- if so, the easy fix is to again to delete the "E".

The "alternate return" feature is considered "obsolescent" by the language standard and could be deleted in a future standard; compilers would likely continue to support it if it were removed because of legacy code. For new code, one simple alternative is to return an integer status variable and use a "select case" statement in the caller.

Upvotes: 2

EvilTeach
EvilTeach

Reputation: 28837

In a Fortran function one returns the value, by assigning the value to a fake variable which is the same name as the function. Once you do that, simply return.

Upvotes: 2

Carl Smotricz
Carl Smotricz

Reputation: 67760

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.

Upvotes: 8

Related Questions