我正在尝试将未知长度的数组传递给函数。我也希望a的索引与b相同。这可能吗?
该程序编译但确实贯穿该函数。
任何帮助都将不胜感激。
function RealCumSum(i) result(j)
real, dimension(1:), intent(in) :: i ! input
real, dimension(size(i)) :: j ! output
integer :: m
do m = 1,size(i)
j(m) = sum(i(1:m))
end do
end function RealCumSum
program xfunc
implicit none
real, dimension(2) :: a = (/ 3.2 , 2.5 /)
real, dimension(2) :: b, RealCumSum
b = RealCumSum(a)
write(*,*) "cumulative sum of ",a," is ", b
end program xfunc
答案 0 :(得分:1)
假定的形状数组参数(dimension(:)
)需要显式接口。最好将程序放在模块中。其他选项是使程序内部(使用contains
)或提供interface
块。
module m
implicit none
contains
function RealCumSum(i) result(j)
real, dimension(1:), intent(in) :: i ! input
real, dimension(size(i)) :: j ! output
integer :: m
do m = 1,size(i)
j(m) = sum(i(1:m))
end do
end function RealCumSum
end module
program xfunc
use m
implicit none
real, dimension(2) :: a = (/ 3.2 , 2.5 /)
real, dimension(2) :: b, RealCumSum
b = RealCumSum(a)
write(*,*) "cumulative sum of ",a," is ", b
end program xfunc