Fortran函数不会将值返回给主程序

时间:2011-11-11 21:10:50

标签: fortran

我从另一个函数integ调用了一个函数rter3。我已从integ函数中打印了integ值,并打印出合法值。但是当我在integ函数处打印rter3值时,会打印0.00。我尽我所能去了解为什么会这样做但却无法理解。请帮帮我。感谢。

function rter3(z,c1,nx1,xarray,xlocation,angl3r,gamr,probr,aaa,da1)
    use data
    integer:: z,o,nx1,lc
    double precision:: c1(nx1),w1,w2,rter3,angl3r,gamr,probr(100),aaa,da1,rati2,xarray(nx1),xlocation,div1
    double precision:: variable
    dx=0.1
    rati2=20.0
    !write(*,*) z,o,nx1,xlocation,angl3,gam
    w1=div1(c1(z-1),c1(z),dx)
    w2=div1(c1(z),c1(z+1),dx)
    variable=integ(nx1,c1,xarray,xlocation,dx,angl3r,gamr,probr,aaa,da1,w1,w2)
    rter3=rati2*variable
    write(*,*) "rter3=", rter3, variable, rati2, xlocation, gam, angl3, da1
end function rter3

function integ(nxx,hfield,xfield,loc1,diff,angl3,gam,prob,afield,dprob,ab2,af2)
    implicit none
    integer :: l,nxx,dui,kk,indx,indt
    double precision:: du(101),prob(100),am1,a,ap1,angl3,gam,diff,dprob,integ,function12,afield(100),xfield(nxx)
    double precision:: loc1,loc2,hfield(nxx),am2,ap2,ab2,af2
    !write(*,*) 'test loop', loc1, diff, angl3, gam, dprob

    do kk=1,100
        do l=1,nxx
            if (0.95*xfield(l)>=loc1-afield(kk).and. 1.05*xfield(l)<loc1-afield(kk)) then
                dui=l
                !call exit()
            endif
        enddo
        !dui= loc1-kk
        !write(*,*) "Entered integ", dui
        !write(*,*) dui
        if (dui<=3) then
            dui=3
        endif
        if (dui>= nxx-2) then
            dui=nxx-2
        endif
        am1=hfield(dui-1)
        am2=hfield(dui-2)
        a=hfield(dui)
        ap2=hfield(dui+2)
        ap1=hfield(dui+1)
        du(kk)=abs(prob(kk)*(function12(am2,am1,ap1,ap2,diff,angl3,gam,ab2,af2))*dprob)
    enddo

    integ=sum(du)
    write(*,*) "integration value=", integ, nxx, loc1, diff, angl3, gam, dprob
end function integ

因此,这些是您参考的个别功能。如果您觉得有问题,请告诉我。整个代码非常大,但如果你觉得你需要看到它,我会附上它。再次感谢。

1 个答案:

答案 0 :(得分:1)

rter3函数如何“知道”积分函数的接口?两者都在同一个模块中吗?如果没有,也许rter3不知道接口并使用隐式类型。由于你没有在rter3中使用“implicit none”,它可能会这样做,在这种情况下,integ的位将被解释为整数,而它们应被解释为双精度 - 该值将显示不正确。我的建议:始终将您的程序放在一个模块中。然后你可以在同一个模块中使用程序......编译器可以检查参数的一致性。如果从主程序或其他模块调用子程序或函数,则“使用”模块以获取参数检查。使用“隐式无”。如果您忘记在源代码中包含隐式none,另外使用执行相同操作的编译器选项(例如,对于gfortran,-fimplicit-none)。