Fortran 基本函数与基本子例程

发布于 2024-11-27 07:32:45 字数 443 浏览 3 评论 0原文

Fortan 允许基本子例程具有intent(inout) 和intent(out) 参数,但基本函数仅允许intent(in)。

这是为什么?这只是一种风格约定,还是调用函数和调用子例程之间存在一般性的不同?

换句话说,

Elemental Integer Function FOO(i)
  Integer, intent(in) :: i
    ...
  FOO=something
End Function

FOO 的这些实现是否同样

Elemental Subroutine FOO(i, v)
  Integer, intent(in)  :: i
  Integer, intent(out) :: v
    ...
  v=something
End Subroutine

有效?

Fortan allows elemental subroutines to have intent(inout) and intent(out) arguments, but elemental functions are only allowed intent(in).

Why is that? Is it just a stylistic convention, or is there something generically different about invoking functions and calling subroutines?

In other words,

Elemental Integer Function FOO(i)
  Integer, intent(in) :: i
    ...
  FOO=something
End Function

and

Elemental Subroutine FOO(i, v)
  Integer, intent(in)  :: i
  Integer, intent(out) :: v
    ...
  v=something
End Subroutine

— are these implementations of FOO equivalently efficient?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

影子的影子 2024-12-04 07:32:46

没有至少一个标记为 intent(out)intent(inout) 的参数的基本子例程是没有意义的,因为您必须以某种方式传递结果。函数有其返回值,子例程必须使用其参数。在 Fortran 2008 中,据我所知,基本过程不必是纯粹的,但仅通过其副作用很难想象一个有用的基本子例程。

There is no point in having an elemental subroutine without at least one argument marked as intent(out) or intent(inout), because you have to pass the result somehow. A function has its return value, a subroutine must use its arguments. In Fortran 2008 AFAIK elemental procedures doesn't have to be pure, but it's hard to imagine a useful elemental subroutine only through its side effects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文