通过引用传递参数到交换函数

发布于 2024-12-13 17:43:22 字数 520 浏览 4 评论 0原文

我在解决练习测试时遇到了这个问题

考虑以下用引用传递语言编写的代码,例如 FORTRAN 和这些关于代码的

  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

语句 语句:

  1. 编译器将生成代码来分配临时无名单元格, 将其初始化为 13,并传递单元交换的地址
  2. 执行时,代码将在 L1 行生成运行时错误
  3. 执行时,代码将在 L2 行生成运行时错误 程序
  4. 将打印 13 和 8
  5. 程序将打印 13 和-2

以上陈述中哪一项是正确的。

我认为S1和S4。有人可以确认一下吗? TIA

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:

  1. The compiler will generate code to allocate a temporary nameless cell,
    initialize it to 13, and pass the address of the cell swap
  2. On execution the code will generate a runtime error on line L1
  3. On execution the code will generate a runtime error on line L2
  4. The program will print 13 and 8
  5. 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

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

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

发布评论

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

评论(2

℡寂寞咖啡 2024-12-20 17:43:22

是的,S1和S4是正确的。

另外,

S0a:该文件无法按原样编译,因为 L1:和 L2:是语法错误。 :)

S0b: 严格来说,将 FORTRAN 说成按引用传递语言是不正确的:

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:

寒冷纷飞旳雪 2024-12-20 17:43:22

扩展@janneb的答案...这是该程序的 Fortran 95 语法正确版本:

module my_subs

contains

subroutine swap(ix,iy)

integer :: ix, iy
integer :: it

it = ix
ix = iy
iy = it

end subroutine swap

end module my_subs

program test_swap

use my_subs

integer :: ia, ib

ia = 3
ib = 8

call swap (ia, ib+5)

print *, ia, ib

end program test_swap

由于这是非法的,因此无法保证输出。使用 gfortran 和 lax 编译器选项,我得到“S4”,即 13 和 8 的输出。

现在将子例程中的参数声明更改为:

integer, intent (inout) :: ix, iy

并且 gfortran 拒绝编译它:

错误:变量定义上下文中的非变量表达式( INTENT = OUT/INOUT 的实际参数) at (1)

与 Intel ifort 类似:

test_swap.f90(31): error #6638:实际参数是表达式或常量;这是无效的,因为关联的虚拟参数具有显式 INTENT(OUT) 或 INTENT(INOUT) 属性。
看涨互换(ia、ib+5)
-----------------^
test_swap.f90 的编译中止(代码 1)

因此,对于正确编写的代码,现代 Fortran 编译器不允许这样做。如果你粗心的话,你可能会犯这个错误,而在 FORTRAN 77 及更早版本中,这个错误要容易得多。

Extending the answer of @janneb... Here is a Fortran 95 syntax-correct version of the program:

module my_subs

contains

subroutine swap(ix,iy)

integer :: ix, iy
integer :: it

it = ix
ix = iy
iy = it

end subroutine swap

end module my_subs

program test_swap

use my_subs

integer :: ia, ib

ia = 3
ib = 8

call swap (ia, ib+5)

print *, ia, ib

end program test_swap

Since this is illegal, the output is not guaranteed. With gfortran and lax compiler options I get "S4", i.e., output of 13 and 8.

Now change the declaration of the arguments in the subroutine to:

integer, intent (inout) :: ix, iy

and gfortran refuses to compile it:

Error: Non-variable expression in variable definition context (actual argument to INTENT = OUT/INOUT) at (1)

Similarly for Intel ifort:

test_swap.f90(31): error #6638: An actual argument is an expression or constant; this is not valid since the associated dummy argument has the explicit INTENT(OUT) or INTENT(INOUT) attribute.
call swap (ia, ib+5)
-----------------^
compilation aborted for test_swap.f90 (code 1)

So with properly written code, modern Fortran compilers do not allow this. You can make this mistake if you are sloppy, and it was much easier in FORTRAN 77 and earlier.

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