2013-07-18 28 views
1

在做下面的積分我不明白應該不是a,b的意圖IN f的意圖是什麼?應該不是子例程中的所有參數變量都有意圖嗎? 這對我來說很困惑,即使它有效我想知道如何?混淆了函數的意圖傳遞給子程序

此外,我看到x是intent(in),因爲y在子例程中作爲f的形式參數傳遞。我只是不確定子程序 的意圖是怎麼回事,它似乎是積分應該是意圖(OUT)的權利?

PROGRAM MAIN 
    IMPLICIT NONE 
    real*10 :: integral 
    a= 1; b =2;n=1000; 
    call simpson(f,a,b,integral,n) 

    REAL*10 FUNCTION f(x)   
     REAL*10, INTENT(IN) :: x 
     f = x**(2.*charge)*exp(-a*x**2 -(b/2)*x**4)   
    END FUNCTION f 


    SUBROUTINE simpson(f,a,b,integral,n)   
    REAL*10   :: integral, a, b 
    REAL*10   :: f 
    REAL*10 h, y ,s         
    integer n, i 
    ! if n is odd we add +1 to make it even 
    if((n/2)*2.ne.n) n=n+1 
    ! loop over n (number of intervals) 
    s = 0.0 
    h = (b-a)/dfloat(n) 
    do i=2, n-2, 2 
     y = a+dfloat(i)*h 
    s = s + 2.0*f(y) + 4.0*f(y+h) 
    end do 
    integral = (s + f(a) + f(b) + 4.0*f(a+h))*h/3.0 
    end subroutine simpson 
     end program main 
+0

怎麼樣了它沒有確定收費工作線的東西的錯誤消息?並且我們不需要CONTAINS聲明? – agentp

回答

2

如果沒有定義,Fortran編譯器識別所有變量爲具有inoutintent

程序參數,例如您的f,是不同的,並且不是真的inoutinout。如果您指定f作爲這類原因,你會得到沿着說的

Error: PROCEDURE attribute conflicts with INTENT attribute in 'f' at (1) 
+1

省略'intent'和'intent(inout)'的參數非常相似但不完全相同......參見http://stackoverflow.com/questions/2880537/fortran-intentinout-versus-omitting-intent –

+0

@MSB:我明白了,謝謝你的鏈接。其中一天,我可能真的讀亞當斯的書:) –