主题:fortran中有关contains语句,external语句,递归函数的一些问题?
各位大侠,小弟初学fortan有一些问题请教。
contains语句定义某些函数只能在某些特定的函数中调用。那么
contains
recursive integer function fact(n)result(ans)
函数fact只能在主程序中调用吗?他能自己递归吗?
external语句用来声明自定义函数,为什么下面的程序中写了integer,external::fact不能通过编译?
externa在遇到contains语句时不用写吗?
elemental函数的参数不能是数组?
比如:
real::a(10)=(/i,i=1,10/)
a=func(a)
elemental real function func(num)
implicit none
real,intent(in)::num
func=sin(num)+cons(num)
return
end function
这个中func(a)这中用法不就是将一个数组作为参数吗?他的意思是不是num=a(1)?或者说不能将数组作为参数只是针对下面的子程序区域。那么我是不是可以写成a=func(a(3:5))从a(3)开始呢?
各位不吝赐教!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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
contains语句定义某些函数只能在某些特定的函数中调用。那么
contains
recursive integer function fact(n)result(ans)
函数fact只能在主程序中调用吗?他能自己递归吗?
external语句用来声明自定义函数,为什么下面的程序中写了integer,external::fact不能通过编译?
externa在遇到contains语句时不用写吗?
elemental函数的参数不能是数组?
比如:
real::a(10)=(/i,i=1,10/)
a=func(a)
elemental real function func(num)
implicit none
real,intent(in)::num
func=sin(num)+cons(num)
return
end function
这个中func(a)这中用法不就是将一个数组作为参数吗?他的意思是不是num=a(1)?或者说不能将数组作为参数只是针对下面的子程序区域。那么我是不是可以写成a=func(a(3:5))从a(3)开始呢?
各位不吝赐教!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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