Fortran return statement

前端 未结 3 1598
情书的邮戳
情书的邮戳 2021-01-20 10:36

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:

         


        
相关标签:
3条回答
  • 2021-01-20 11:16

    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.

    0 讨论(0)
  • 2021-01-20 11:18

    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.

    0 讨论(0)
  • 2021-01-20 11:30

    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.

    0 讨论(0)
提交回复
热议问题