欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

fortran2003新特性:可分配函数

程序员文章站 2022-03-09 09:57:36
...
Module testMod
    implicit none
contains    
    
    function testfunc( a ) result( res )
        implicit none
        real, allocatable :: res(:)
        integer           :: i 
        real              :: a(:)
        
        if ( allocated( res ) ) then
            write( *,'(1x,a)' )  'the distributable function is allocated!'
            deallocate( res )
            !.. reallocate
            allocate( res(size(a)) )
        else
            write( *,'(1x,a)' )  'the distributable function is not allocated!'
            allocate( res(size(a)) )
        end if
        
        forall ( i = 1:size(res) )
            res(i) = a(i) * 10.0
        end forall
        
    end function testfunc
    
End module testMod
    
        
Program main
    use testMod
    implicit none
    real :: a(5)
    
    call random_seed
    call random_number( a )
    write( *,'(*(g0,3x))' ) testfunc(a)
    
End program main

 

相关标签: fortran