title: Declaration of dummy argument explicit shape arrays in Fortran
body: <p>I thought that the specification of an explicit shape dummy argument array in a subroutine can involve any integer variables, including other dummy variables (usual cases), module variables, and local variables of the present subroutine. But it turns out that local variables (which are not dummy variables) can not be used in the specification.</p>
  
  <p>An example is as follows:</p>
  
  <pre><code>module mp
  implicit none
  contains
    subroutine p(b)
      integer :: m=4, n=4 !not integer,parameter :: m=4, n=4
      integer :: b(m,n)
    end subroutine p
  end module mp
  </code></pre>
  
  <p>gfortran will raise <code>Error: Variable 'm' cannot appear in the expression at (1)</code></p>
  
  <p>For this example, I can use <code>integer,parameter :: m=4, n=4</code> to avoid this, but I do not understand why the original case does not work, considering the fact that the bounds/extents of explicit shape arrays do not need to be known at compile time. A modified version of the above example works:</p>
  
  <pre><code>module mp
  implicit none
  integer :: m=4, n=4
  contains
    subroutine p(b)
      integer :: b(m,n)
    end subroutine p
  end module mp
  </code></pre>
  
  <p>Considering the slight difference between the two examples, I expect both of them work, but in fact the former does not. Could someone explain the reason?</p>
  
  <p>Update: I found out that this is a very subtle issue because it depends on whether the subroutine is contained in a module or is standalone, it also depends on the version of gfortran. I have posted examples in the answer region.</p>