Fortran函数错误,与包含范围单元中的名称冲突
我刚学Fortran。这个简单的代码有什么问题Fortran函数错误,与包含范围单元中的名称冲突,fortran,fortran90,Fortran,Fortran90,我刚学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 Fac
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)
由于
事实
在程序中是包含
的,编译器将自动生成一个接口。通过声明一个名为Fact
的integer
东西,你给编译器的指令是冲突的,它不喜欢这样。只需从行中删除事实
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
子句,否则编译器的行为就像创建了一个与函数同名的变量来返回函数的结果一样