fortran goto scope

2019-05-06 15:09发布

问题:

I have a legacy fortran code with many statements like 'goto 50'. I was wondering whether the target of goto is global or local. I mean, if multiple functions have a target '50', where does the goto leads to.

Thanks for answering.

回答1:

The statement labels (eg, "50") have to be defined within the current "scoping unit", which basically translates in this context to the subroutine/function that the goto call is in (or the main program, if the call is in the main program).

So for instance, in the following program, the main program and both contained subroutines have their own label 50, and the gotos go to "their" line 50.

program testgotos
    implicit none

    goto 50
    call second
 50 call first
    call second

contains

    subroutine first
    integer :: a = 10

    goto 50
    a = 20
 50 print *,'First: a = ', a

    end subroutine first

    subroutine second
    integer :: a = 20

    goto 50
    a = 40
 50 print *,'Second: a = ', a

    end subroutine second

end program testgotos


回答2:

Local.

Technically from the f77 standard ( http://www.fortran.com/fortran/F77_std/f77_std.html )

"Statement labels have a scope of a program unit."



标签: fortran goto