Fortran pass an array to function

前端 未结 1 1336
执念已碎
执念已碎 2021-01-23 18:51

I am trying to pass an array of unknown length to a function. I would also prefer the indices of a is the same as b. Is this possible?

The program compiles but does run

相关标签:
1条回答
  • 2021-01-23 19:21

    The assumed shape array arguments (dimension(:)) require an explicit interface. It is best done by placing the procedure in a module. The other options are to make the procedure internal (using contains) or to supply an interface block.

    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
    
    0 讨论(0)
提交回复
热议问题