How to call Array-valued Functions in fortran?

前端 未结 1 383
夕颜
夕颜 2020-12-04 00:52

I want to write a function that returns an allocatable array in fortran

program test
    implicit none
    real a(3)
    real, allocatable :: F18(:)
    a =          


        
相关标签:
1条回答
  • 2020-12-04 01:14

    You need to make the properties of the function known to the caller. The easiest way is to put it into a module and 'use' that module. In your examples, in your main program you are declaring an array 'F18', which is not the function.

    module mystuff
    
    contains
    
    function F18(A,n)
    implicit none
        integer n
        real A(:)                   ! An assumed shape array
        real F18(size(A,1))         ! The function result itself is
                                   ! the second dimension of A.
        F18 =A                 !
    end function F18
    
    
    end module mystuff
    
    program test
        use mystuff
        implicit none
        real a(3)
        a = (/1,2,3/)
        print *, F18(a,3)
    end program test
    
    0 讨论(0)
提交回复
热议问题