! this program demonstrates how to pass variable size matrices to subroutines module tr implicit none contains function trace(a) ! compute trace of matrix a real :: a(:,:), trace, sum=0. integer :: i do i=1,size(a,1) ! size(a,1) and size(a,2) are dimensions of a sum=sum+a(i,i) end do trace=sum end function end module program trtest ! compute trace of Hilbert matrix use tr implicit none integer :: n, i, j real, allocatable :: a(:,:) print *, 'n =' read *, n allocate(a(n,n)) do i=1,n do j=1,n a(i,j)=1./(i+j-1) end do end do print *, 'trace =', trace(a) end program