I have a question that I am trying to work through. I have a program:
1. program Pass
2. var b:integer; a:integer; v :array[5] of integer ;
3. procedure P (x:integer, y:integer ) ;
4. var z:integer ;
5. begin
6. z := x ;
7. x := y ;
8. y := z ;
9. end
10. begin
11. b := 1; a := 3; v := [ 2,1,4,5,4 ] ;
12. P( a, v [ a ] ) ;
13. end
And I am asked what are the values of b, a and v after call by reference and call by value-result. The array here also starts at index 1.
Solution: Call By Reference
For call by reference we learned that the actual parameters are aliased with the formal parameters (hopefully that wording is correct?) and actual parameters can be updated during execution of the code. For my solution I have:
- alias a and x. a is set to 3;
- alias v[a = 3] and y. v[a = 3] = 4 (indexing starts at one).
- z = x = a = 3;
- x = y = v[a = 3] = 4;
- a is updated to 4;
- v[a = 4] = y = z = 3;
- v is [2, 1, 4, 3, 4].
So I have b = 1, a = 4; and v = [2, 1, 4, 3, 4], but the solution says v = [2, 1, 3, 5, 4]. Can someone help me understand why, if a is updated during the execution then why the solution has v[3] still being updated? I feel like I am missing a key piece of the mechanism here.
Solution: Call By Value-Result
For call by value-result we learned that parameters are copied in and then they are copied out after the code has executed. Here are the steps again that I took to solve the problem:
- a = 3; passed into x; x = 3;
- v[a = 3] = 4 (indexed by 1) and passed into y; y = v[a = 3] = 4;
- z = x = 3;
- x = y = 4;
- y = z = 3;
- x = 4 and y = 3 are copied out to a and v[a] which again is v[a = 4] now? I was told the answer below is correct but another question for me came up in what order are the values copied back? If v[a] was copied back first before a was changed then doesn't copy order play a role in how the values are updated?
So b = 1; a = 4; v = [2, 1, 4, 3, 4].
If anyone could help me sort out these thoughts I would greatly appreciate it.
1 Answer 1
Just to clean up some confusion: What you call "call by reference" was "call by name" in Algol. In newer languages, "call by reference" will take the address of an object and the function accesses the object through the reference. Call by value/result locates the object, passes a value, gets a value back and stores it in the same variable.
Swift has "auto closure" arguments that are passed as code that will be executed by the function allowing the same things as "call by name".
In your first case you seem to evaluate what Algol "call by name" would do - the language also looks like Algol to me (distant memory). The solution you refer to would be a modern "call by reference" result, like you would get in C++.
In the second case, you are right, the result would be unspecified if v[a] was evaluated again. Most likely it would not.
Explore related questions
See similar questions with these tags.
Pprocedure swaps its parameters and should be calledswap. (2) Thezvariable should be calledtemp. (3) Why doesbeven exist when it is never referenced? $\endgroup$