Fortran Coder

查看: 835|回复: 1
打印 上一主题 下一主题

[子程序] 实例0831书中说不需要声明就可以用,但是出现报错

[复制链接]

1967

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1370 元
贡献
581 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

楼主
发表于 2023-10-13 17:27:05 | 显示全部楼层
去掉 stop 后面的 end

被主程序包含的 fact 函数需要放在program 里面,才能被 program 识别,实现不定义而使用。

此外,stop 其实也没有存在的必要。return 没必要存在
再次此外,当 fact 包含在 program 里面时,因为 program 中已经有 implicit none 了,所以 fact 函数里的 implicit none 也没必要存在。

[Fortran] 纯文本查看 复制代码
program ex0831
  implicit none
  integer ::n
  write(*,*)'N='
  read (*,*)n
  write(*,"(I2,'!=',I8)")n,fact(n)

contains

  recursive integer function fact(n) result(ans)
    integer ,intent(in)::n
    if (n<0)then
      ans=-1
    else if (n<=1)then
      ans =1
    else
      ans=n*fact(n-1)    
    end if
  end function fact
  
end program ex0831

您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-14 22:35

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表