[Fortran] 纯文本查看 复制代码
program recursive_test
implicit none
INTEGER :: n
interface
RECURSIVE INTEGER function fact(n) result(ans)
LOGICAL :: count
INTEGER :: n
end function fact
end interface
write (*, "('N = ', I3)")
read (*, *) n
write (*, "(I3, '! = ', I8)") n, fact(n)
stop
end program recursive_test
RECURSIVE INTEGER function fact(n) result(ans)
implicit none
INTEGER :: n
LOGICAL :: count
if (n < 0) then
ans = -999
count = .false.
return
else if (n <= 1) then
ans = 1
count = .true.
return
end if
ans = n*fact(n - 1)
return
end function fact
[Fortran] 纯文本查看 复制代码
program recursive_test
implicit none
INTEGER :: n
interface
RECURSIVE INTEGER function fact(n)! result(ans)
!LOGICAL :: count 不必要
INTEGER :: n
end function fact
end interface
write (*, "('N = ', I3)")
read (*, *) n
write (*, "(I3, '! = ', I8)") n, fact(n)
! stop 不必要
end program recursive_test
RECURSIVE INTEGER function fact(n) result(ans)
implicit none
INTEGER :: n
!LOGICAL :: count
if (n < 0) then
ans = -999
!count = .false.
!return
else if (n <= 1) then
ans = 1
!count = .true.
!return
else
ans = n*fact(n - 1)
end if
!return
end function fact