title: How to keep variables defined in a recursive Fortran subroutine from updating upon internal calls?
body: <p>With the following code, when variable <code>a</code> and <code>a1</code> are equal to 2, I would like the recursive subroutine to create 2 nested do loops of length 4 (maxdev=4).</p>
  
  <p>I am attempting to control the level of nesting by the variable <code>count</code> (<code>count1</code> in the subroutine).  However, when the subroutine is called by itself, the value for <code>count1</code> is not preserved within the first call.  I would like the outer loop to retain a value for <code>count1</code> of 1 and the second loop to have a value for <code>count1</code> of 2, to reflect the level of nesting. </p>
  
  <p>I am unsure how to specify the program so this is achieved.  Instead, when the call that creates the inner loop returns to the outer loop, the value of <code>count1</code> in the outer loop has been changed and reflects what it incremented to be in the inner loop.</p>
  
  <pre><code>program fragarrays
  implicit none
  
  integer::gridres,maxdev,a,count
  integer, allocatable:: x(:)
  
  open(unit=1,file='D:/output11.txt',status='unknown')
  
  gridres=2
  
  maxdev=gridres*gridres
  
  do a = 1,2 
      count=0    
      allocate(x(a))
      call assigncell(a,maxdev,count,x)
      deallocate(x)
  end do
  
  contains
  
  recursive subroutine assigncell(a1,maxdev1,count1,x1)
      integer:: a1,maxdev1,b
      integer::count1
      integer,dimension(a1):: x1
  
      count1=count1+1
      do b=1,maxdev1
          x1(count1)=b
          write (1,*)count1,x1(count1),b,a1
          if(count1.lt.a1)then
              call assigncell (a1,maxdev1,count1,x1)
          end if
  
      end do
  
  end subroutine assigncell
  
  end program fragarrays
  </code></pre>