Welcome to AE Resources
Converted document
PROGRAM aarray
INTEGER, PARAMETER :: length = 5
INTEGER :: i
REAL :: a(length)
​
! Add this interface to the routine
INTERFACE
     SUBROUTINE test(b)
     REAL, DIMENSION(:) :: b
     END SUBROUTINE 
END INTERFACE
​
DO i=1,5
  a(i) = 2.*i
ENDDO
​
WRITE(*,*) ’MAIN PROGRAM’
WRITE(*,*) a
​
CALL test(a)
​
END PROGRAM aarray
​
SUBROUTINE test(b)
REAL, DIMENSION(:) :: b
​
WRITE(*,*) ’IN SUBROUTINE’
WRITE(*,*) b
​
END SUBROUTINE test
 
This code, when run outputs the following
MAIN PROGRAM
   2.0000000   4.0000000   6.0000000   8.0000000  10.0000000 
IN SUBROUTINE
   2.0000000   4.0000000   6.0000000   8.0000000  10.0000000 
   
The use of the CONTAINS statement is no longer needed in the example above, and the END PROGRAM statement is moved.
If the n dimension parameter is passed as well, then the INTERFACE block must be modified to read
INTERFACE 
     SUBROUTINE test(n,b) 
     REAL, DIMENSION(n) :: b 
     END SUBROUTINE 
END INTERFACE
Here, the size of the array used in the subroutine is dimensioned with n and not : in the INTERFACE block.
There are other methods to pass this information efficiently, and these will be discussed in the MODULE article.
← Previous Page
← Previous Page