Fortran函数错误:与包含作用域单元中的名称冲突

3

我是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)
1个回答

6

由于Fact已经被包含在程序中,编译器会自动生成一个接口。如果你还声明了一个叫做Fact的整数变量,就会给编译器造成冲突,它不喜欢这样。所以请从代码中删除Fact

integer :: m, n, 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

除非在function语句上使用result子句,否则编译器将表现为创建与函数同名的变量以返回函数的结果。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接