0

我有一个练习题让我在即将到来的认证考试中遇到了困难。无论如何,请提供帮助!我想我了解如何获得答案的按值传递部分,但对这个问题的按引用传递部分没有任何想法。

procedure calc ( pass-by-value int w,
                 pass-by-value int x,
                 pass-by-reference int y,
                 pass-by-reference z)

    w <-- w + 1
    x <-- x * 2
    y <-- y + 3
    z <-- z * 4

end procedure

下面代码片段末尾的 a 和 b 的值是多少?

int a <-- 5
int b <-- 6
calc (a, a, b, b)
4

2 回答 2

1

a is never changed outside the procedure because it's passed by value, while b will be changed, because it's passed by reference. Assignment to variables passed by reference will remain outside the procedure.

one way to look at it is to substitute reference arguments by the caller variable, substitute y,z by b. while no substitute for a because it's called by value.

now your code will exactly look like this if w,x passed by value y,z by reference: a will be 5 no change while b will be:

int a <-- 5
int b <-- 6
w <-- a + 1
x <-- a * 2
b <-- b + 3   => b will be 9 
b <-- b * 4   => b will be 36

b will be 36 inside the procedure and after return of the procedure.

于 2014-06-14T04:11:20.543 回答
0

Results:

w = 6, x = 10, y = 9, z = 36

After calculations a = 5 and b = 36

于 2014-06-14T04:13:42.067 回答