在使用Fortran语言写程序时,经常会将经常使用到的功能写成自定义函数或子程序,使用时直接调用,有时需要传递数组,但传递前不知道数组的具体大小,需要子程序来进行确定,所以实现子程序参数是可变数组的功能要将子程序放在module中,具体如下。
module m_ceshi
implicit none
contains
subroutine ceshi(array)
implicit none
integer,allocatable::array(:)
allocate(array(3))
array=(/1,2,3/)
return
end subroutine ceshi
end module m_ceshi
program main
use m_ceshi
implicit none
integer,allocatable::array1(:)
call ceshi(array1)
write(*,*)array1
stop
end program main
以上为测试代码,在vs+ivf中测试过可直接运行,主程序将不知道大小的可变数组传入子程序,子程序接收可变数组,确定其维度,并赋值后返回。