2014-10-09 26 views
2

我是Fortran中的新成員。這個簡單的代碼有什麼問題?Fortran與包含作用域單元中的名稱衝突的函數錯誤

program combinatorial 
    Implicit none 
    integer :: m, n, Fact 
    integer :: Com 
    Write (*,*) 'inter 2 number for m and n' 
    Read (*,*) m,n 
    Com = Fact (m)/(Fact(n)*Fact(m-n)) 

    Contains 
    integer Function Fact(t) 
     Implicit none 
     Integer, intent(IN) :: t 
     integer :: i, Ans  
     Ans = 1 
     Do i=1, t 
      Ans=Ans * i 
     End do 
     Fact = Ans 
    End Function Fact 
End program combinatorial 

,我遇到的錯誤是:

combinatorial.f90(10): error #6626: The name of the internal procedure conflicts with a name in the encompassing scoping unit. [FACT] 
    integer Function Fact(t) 
-------------------------^ 
compilation aborted for combinatorial.f90 (code 1) 

回答

5

Fact由於是在程序內contain編編譯器會自動生成到它的接口。通過還聲明一個integer東西叫做Fact你給編譯器提供了衝突的指令,它不喜歡那樣。距離線

integer :: m, n, Fact 

encompassing scoping unit由編譯器稱爲下降Fact是包含程序(或包括)的功能。

另外,您不需要在函數的定義中使用變量Ans。你可以簡單地寫

integer Function Fact(t) 
    Implicit none 
    Integer, intent(IN) :: t 
    integer :: i  
    Fact = 1 
    Do i=1, t 
     Fact = Fact * i 
    End do 
End Function Fact 

除非你使用result條款上function聲明編譯器的行爲就好像它返回函數的結果產生的函數同名的變量。