Scoping rules for variable and functions in contained subroutines

前端 未结 1 2049
囚心锁ツ
囚心锁ツ 2020-12-21 17:24

I have a problem understanding why a variable (i) declared in a subroutine is seen in a contained subroutine, but that this is not true for a function (fi

相关标签:
1条回答
  • 2020-12-21 17:57

    The simplest fix is to use

    double precision, external :: fie
    

    the external attribute (also can be specified by the external statement) says: this is a procedure, I am not declaring a local variable.

    For the declaration without the external to be interpreted as a function declaration the function reference must be present within the function body. Internal functions don't count. And therefore the compiler created a local double precision variable called fie.

    Thank's to IanH for the relevant standard rule (from Fortran 2008 (16.5.1.4p5), but Fortran 95 will have an equivalent):

    If an external or dummy procedure with an implicit interface is accessed via host association, then it shall have the EXTERNAL attribute in the host scoping unit; if it is invoked as a function in the inner scoping unit, its type and type parameters shall be established in the host scoping unit. The type and type parameters of a function with the EXTERNAL attribute are established in a scoping unit if that scoping unit explicitly declares them, invokes the function, accesses the function from a module, or accesses the function from its host where its type and type parameters are established.

    Of course explicit interfaces (best using modules) are much better than external functions.

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