Fortran 基本函数与基本子例程
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有至少一个标记为
intent(out)
或intent(inout)
的参数的基本子例程是没有意义的,因为您必须以某种方式传递结果。函数有其返回值,子例程必须使用其参数。在 Fortran 2008 中,据我所知,基本过程不必是纯粹的,但仅通过其副作用很难想象一个有用的基本子例程。There is no point in having an elemental subroutine without at least one argument marked as
intent(out)
orintent(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.