Fortran Coder

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

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

[复制链接]

12

帖子

4

主题

0

精华

入门

F 币
46 元
贡献
27 点
跳转到指定楼层
楼主
发表于 2023-10-13 16:06:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
[Fortran] 纯文本查看 复制代码
program ex0831
implicit none
integer ::n
write(*,*)'N='
read (*,*)n
write(*,"(I2,'!=',I8)")n,fact(n)
stop
end

contains
recursive integer function fact(n)result(ans)
implicit none
integer ,intent(in)::n

if (n<0)then
ans=-1
return
else if (n<=1)then
ans =1
return
end if
ans=n*fact(n-1)
return
end function fact
end

--------------------Configuration: 21351 - Win32 Debug--------------------
Compiling Fortran...
E:\Fortran\MSDEV98\MyProjects\21351\320020.f90
E:\Fortran\MSDEV98\MyProjects\21351\320020.f90(6) : Error: This name does not have a type, and must have an explicit type.   [FACT]
write(*,"(I2,'!=',I8)")n,fact(n)
-------------------------^
Error executing df.exe.

21351.exe - 1 error(s), 0 warning(s)


分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

1963

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1357 元
贡献
574 点

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

沙发
发表于 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-4-29 09:14

Powered by Tencent X3.4

© 2013-2024 Tencent

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