Pit*_*rog 5 arrays fortran fortran95
考虑以下子程序
subroutine myProc(m,n,flag,X)
Integer, intent(in) :: m,n
logical, intent(in) :: flag
real(8), intent(out), allocatable :: X(:,:)
if (flag) then
allocate(X(m,n))
! some more code here
else
allocate(X(m-1,n))
! some more code here
end if
end subroutine myProc
!!!!!!!!!!!!!!!!!!!
Run Code Online (Sandbox Code Playgroud)
另外,如何在程序中调用此过程?假设我写
!... some code before
call myProc(5,6,.TRUE.,X)
Run Code Online (Sandbox Code Playgroud)
我是否需要将 X 定义为 (4,6) 实数数组或将可分配数组传递给子例程?
在 Fortran 95 中甚至可以做到所有这些吗?
Dan*_*Sp. -4
您可以将 X 放入模块中,在调用程序中声明它并在子例程中分配它。使用此类模块的任何程序、函数或子例程都会引用相同的内存空间,因此这些变量甚至不会作为参数传递。它看起来像这样:
module Global
real(8), allocatable :: X(:,:)
end module Global
program main
use Global
!... some code before
call myProc(5,6,.TRUE.)
end program main
subroutine myProc(m,n,flag)
use Global
Integer, intent(in) :: m,n
logical, intent(in) :: flag
if (flag) then
allocate(X(m,n))
! some more code here
else
allocate(X(m-1,n))
! some more code here
end if
end subroutine myProc
!!!!!!!!!!!!!!!!!!!
Run Code Online (Sandbox Code Playgroud)