通过引用传递参数传递交换函数 [英] Passing arguments by pass-by-reference to a swap function
问题描述
我在解决练习测试时遇到了这个问题。考虑下面的代码以
FORTRAN之类的传递引用语言编写,并且这些语句代码
子程序交换(ix,iy)
it = ix
ix = iy! L1行
iy = it!在线L2
结束
程序主要
ia = 3
ib = 8
调用交换(ia,ib + 5)
print * ,ia,ib
最终计划
陈述:
- 编译器将生成代码来分配一个临时无名单元,
将其初始化为13,并传递单元交换的地址
- 执行时,代码将在第L1行生成运行时错误。
- 执行时,代码将在第二行生成运行时错误。程序将打印13和8
- 程序将打印13和-2
上面的声明是正确的。
我认为S1和S4。任何人都可以确认吗? TIA
是的,S1和S4都是正确的。
,
S0a:由于L1:和L2:是语法错误,所以文件不可编译。 :)
S0b:严格来说,FORTRAN作为参考传递语言是不正确的:
-
语言的名称是Fortran
-
按引用传递的是未在标准中指定的实现细节。拷入,拷出是另一种可能的实现。此外,一些较新的Fortran语言功能(F90 +)实际上意味着既不是通过引用也不是copy-in-copy-out的实现。见例如指针作为虚拟参数和 http:// software .intel.com / en-us / blogs / 2009/03/31 / doctor-fortran-in-iive-come-here-for-an-argument /
I encountered this problem while solving a practice test
Consider the following code written in a pass-by-reference language like FORTRAN and these statements about the code
subroutine swap(ix,iy)
it = ix
ix = iy ! line L1
iy = it ! line L2
end
program main
ia = 3
ib = 8
call swap (ia, ib+5)
print *, ia, ib
end program
Statements:
- The compiler will generate code to allocate a temporary nameless cell, initialize it to 13, and pass the address of the cell swap
- On execution the code will generate a runtime error on line L1
- On execution the code will generate a runtime error on line L2
- The program will print 13 and 8
- The program will print 13 and -2
Which of the above the above statement(s) is/are correct.
I think S1 and S4. Could anyone please confirm? TIA
Yes, S1 and S4 are correct.
Also,
S0a: The file is not compilable as is because the L1: and L2: are syntax errors. :)
S0b: Strictly speaking, speaking of FORTRAN as a pass-by-reference language is incorrect:
The name of the language is Fortran
Pass by reference is an implementation detail that is not specified in the standard. Copy-in, copy-out is another possible implementation. Also, some newer Fortran language features (F90+) in practice imply an implementation which is neither pass-by-reference nor copy-in-copy-out. See e.g. pointer as a dummy argument and http://software.intel.com/en-us/blogs/2009/03/31/doctor-fortran-in-ive-come-here-for-an-argument/
这篇关于通过引用传递参数传递交换函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!