Kar*_*våg 18 fortran arguments
在书籍中搜索了一段时间后,在stackoverflow和一般网络上,我发现很难找到对fortran参数意图之间真正差异的直接解释.我理解它的方式是这样的:
intent(in) - 实际参数被复制到条目处的伪参数.intent(out) - 伪参数指向实际参数(它们都指向内存中的相同位置).intent(inout) - 伪参数在本地创建,然后在过程完成时复制到实际参数.如果我的理解是正确的,那么我也想知道为什么人们想要使用intent(out),因为intent(inout)需要更少的工作(不复制数据).
小智 21
意图只是编译器的提示,你可以抛弃那些信息并违反它.意图几乎完全存在,以确保您只执行您计划在子例程中执行的操作.编译器可能会选择信任您并优化某些内容.
这意味着intent(in)不是通过值传递.您仍然可以覆盖原始值.
program xxxx
integer i
i = 9
call sub(i)
print*,i ! will print 7 on all compilers I checked
end
subroutine sub(i)
integer,intent(in) :: i
call sub2(i)
end
subroutine sub2(i)
implicit none
integer i
i = 7 ! This works since the "intent" information was lost.
end
program xxxx
integer i
i = 9
call sub(i)
end
subroutine sub(i)
integer,intent(out) :: i
call sub2(i)
end
subroutine sub2(i)
implicit none
integer i
print*,i ! will print 9 on all compilers I checked, even though intent was "out" above.
end
Run Code Online (Sandbox Code Playgroud)
intent(in) - 看起来像传递值(并且它的更改不会反映在外部代码中),但实际上是通过引用传递,并且编译器禁止更改它.但它仍然可以改变.intent(out) - 以某种方式通过引用传递,实际上是一个返回参数intent(inout) - 通过引用传递,正常输入/输出参数.使用intent(out)if是明确的,记录您的设计.如果有的话,不要关心很少的性能提升.(评论表明没有,因为intent(in)技术上也通过引用传递.)
| 归档时间: |
|
| 查看次数: |
12050 次 |
| 最近记录: |