title: Smart way to pass arguments in Fortran 90
body: <p>I am a Fortran novice.  I am trying to write a subroutine that will take in four arguments from the main program, and then outputs to the main program an array that involves the four arguments that were originally passed in.  What is a good/smart way to do this?</p>
  
  <p>For example, in my test program below, I create four real variables (<code>a</code>, <code>b</code>, <code>c</code>, and <code>d</code>) in the main program.  Then I pass these real variables to a subroutine called <code>mysub</code>.  I would like <code>mysub</code> to be able to take in <code>a</code>, <code>b</code>, <code>c</code>, and <code>d</code>, use them to populate a 2-by-2 array called <code>o</code>, and then send <code>o</code> to the main program for displaying (and possible modification) there.  So, I tried the following:</p>
  
  <pre><code>SUBROUTINE mysub(w,x,y,z)
    IMPLICIT NONE
    REAL, INTENT(IN) :: w, x, y, z
    REAL, DIMENSION(:,:), ALLOCATABLE, INTENT(OUT) :: o
  
    ALLOCATE(o(2,2))
    o(1,1)=w
    o(1,2)=x
    o(2,1)=y
    o(2,2)=z
  END SUBROUTINE mysub
  END MODULE testsubs
  
  PROGRAM test
    USE testsubs
    IMPLICIT NONE
    REAL :: a=1.1, b=2.2, c=3.3, d=4.4
  
    CALL mysub(a, b, c, d)
  
    PRINT *, o(1,1), o(1,2)
    PRINT *, o(2,1), o(2,2)
  END PROGRAM test
  </code></pre>
  
  <p>But, I get the following error: </p>
  
  <pre><code>test.f90:10.53:
  
    REAL, DIMENSION(:,:), ALLOCATABLE, INTENT(OUT) :: o
                                                       1
  Error: Symbol at (1) is not a DUMMY variable
  </code></pre>
  
  <p>I interpret this as, the compiler doesn't know what <code>o</code> is, because <code>o</code> is not in the list of arguments in the subroutine header: <code>SUBROUTINE mysub(w,x,y,z)</code>.  So I probably need to include <code>o</code> in that header.  So, I next try the following (where I have denoted changes or additions using <code>!...</code>):</p>
  
  <pre><code>SUBROUTINE mysub(w,x,y,z,o) !...
    IMPLICIT NONE
    REAL, INTENT(IN) :: w, x, y, z
    REAL, DIMENSION(:,:), ALLOCATABLE, INTENT(OUT) :: o
  
    ALLOCATE(o(2,2))
    o(1,1)=w
    o(1,2)=x
    o(2,1)=y
    o(2,2)=z
  END SUBROUTINE mysub
  END MODULE testsubs
  
  PROGRAM test
    USE testsubs
    IMPLICIT NONE
    REAL :: a=1.1, b=2.2, c=3.3, d=4.4
    REAL, DIMENSION(:,:), ALLOCATABLE :: o !...
  
    CALL mysub(a, b, c, d, o) !...
  
    PRINT *, o(1,1), o(1,2)
    PRINT *, o(2,1), o(2,2)
    DEALLOCATE(o) !...
  END PROGRAM test
  </code></pre>
  
  <p>This seems to work fine, and I get the correct output:</p>
  
  <pre><code>   1.1000000       2.2000000
     3.3000000       4.4000001
  </code></pre>
  
  <p>But, my question is, is this a good way to do this?  In this working example, I'm declaring the array <code>o</code> <em>both</em> in the subroutine <em>and</em> in the main program.  This seems potentially confusing, because I think that this means that I need to take care that <em>either</em> the subroutine <em>or</em> the main program allocates <code>o</code> (but <em>not</em> both, I think, in order to avoid error messages).  Is there a smarter way to do this--to send an array from a subroutine to the main program?  Thank you for your time.</p>