我是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
Run Code Online (Sandbox Code Playgroud)
我遇到的错误是:
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)
Run Code Online (Sandbox Code Playgroud)
由于Fact是contain在程序中编编译器会自动生成到它的接口.通过声明一个integer称为Fact你给编译器冲突的指令而且它不喜欢的东西.刚从Fact线上掉下来
integer :: m, n, Fact
Run Code Online (Sandbox Code Playgroud)
在encompassing scoping unit由编译器称为是包含(或程序包括)的功能.
而且,顺便说一句,您不需要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
Run Code Online (Sandbox Code Playgroud)
除非result在function语句中使用子句,否则编译器的行为就像它创建一个与返回函数结果的函数同名的变量.