Fortran Coder

查看: 16186|回复: 3
打印 上一主题 下一主题

[求助] 子例行程序

[复制链接]

954

帖子

0

主题

0

精华

大师

F 币
184 元
贡献
75 点

规矩勋章元老勋章新人勋章水王勋章热心勋章

QQ
楼主
发表于 2021-4-18 16:19:34 | 显示全部楼层
程序单元里不能直接放入另一个程序单元,需要符合一定的规则。

[Fortran] 纯文本查看 复制代码
program hello
  implicit none
  real :: ss,s(4)

  call fcount(50,s(1))
  call fcount(100,s(2))
  call fcount(150,s(3))
  call fcount(200,s(4))
  ss=sum(s)
  print *, "s=", ss

contains

subroutine fcount(n,s)
  integer :: n , i
  real :: s
  s=0
  do i=1,n
    s=s+1./i
  end do
end subroutine fcount

end program hello

954

帖子

0

主题

0

精华

大师

F 币
184 元
贡献
75 点

规矩勋章元老勋章新人勋章水王勋章热心勋章

QQ
沙发
发表于 2021-4-18 16:32:49 | 显示全部楼层
考虑到 Fortran 是一门向量化的语言,给出一个高级的版本,非常简单且高效。

[Fortran] 纯文本查看 复制代码
program hello
  implicit none
  write(*,*) "s=", sum(fcount([50,100,150,200]))

contains

Elemental Real Function fcount(n) result( s )
  integer , intent(IN) :: n
  integer :: i
  s=sum(1./[(i,i=1,n)])
End Function fcount

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

本版积分规则

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

GMT+8, 2024-5-16 03:23

Powered by Tencent X3.4

© 2013-2024 Tencent

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