下面的程序加了external之后不能通过编译,去掉之后可以运行,这个程序是彭国伦那本书的201页,我有疑问,这个程序的子函数fact为什么在主程序中用externa语句声明?如果是自定义函数不是都在主程序中必须声明么?
还有contains 可以设置一个内部函数,如果一个函数被设置成内部函数,那么他就不能自己递归么?
这个程序是应为有了contains才不用external吗?
总之在自定义函数有interface和contains的情况下都不写external吗?
小弟刚学fortran,概念不清晰,请各位大侠明示!
program pp1
implicit none
integer ::n
integer,external::fact
write(*,*)'N,'
read(*,*)n
write(*,"(I2,'!=',I8)"n,fact(n)
pause
contains
recursive integer function fact(n)result(ans)
implicit none
integer,intent(in)::n
integer,save::count=1
integer::localcount,temp
localcount=count
count=count+1
write(6,"(I2,'th enter,n=',I2)"localcount,n
if(n<0)then
ans=-1
write(6,"(I2,'th exit,n=',I2,' ans=',I8)"localcount,n,ans
return
else if(n<=1)then
ans=1
write(6,"(I2,'th exit,n=',I2,' ans=',I8)"localcount,n,ans
return
end if
temp=n-1
ans=n*fact(temp)
write(6,"(I2,'th exit,n=',I2,' ans=',I8)"localcount,n,ans
return
end function fact
end program pp1
[ Last edited by chenhuis on 2011-11-3 at 21:31 ] |