Programming and Software Development: Question 3
Syllabus 11.3
A programmer writes this procedure, intending it to swap the values held in two variables in the main program.
PROCEDURE Swap(BYVAL A : INTEGER, BYVAL B : INTEGER)
DECLARE Temp : INTEGER
Temp ← A
A ← B
B ← Temp
OUTPUT "Inside Swap: A = ", A, " B = ", B
ENDPROCEDURE
// Main program
DECLARE X : INTEGER
DECLARE Y : INTEGER
X ← 5
Y ← 9
CALL Swap(X, Y)
OUTPUT "After call: X = ", X, " Y = ", Y
(a) Copy and complete a trace table showing the value of A, B and Temp after each of the
three assignment statements inside Swap, and state the values output by the OUTPUT statement
inside Swap. [3]
(b) State the values output by the final line, OUTPUT "After call: X = ", X, " Y = ", Y, and
explain, in terms of how a BYVAL parameter works, why these values are not the swapped values
seen inside Swap. [2]
(c) State the one change needed to the header line of Swap so that calling CALL Swap(X, Y)
actually swaps the values stored in X and Y in the main program, and explain why this change
achieves a real swap. [2]
Show worked solution Hide worked solution
Worked solution
Part (a): Tracing the BYVAL call
Swap(X, Y) is called with X = 5 and Y = 9. Because both parameters are declared BYVAL, A
and B are separate local variables that each receive a copy of the value passed in: A
starts as 5, B starts as 9.
| Statement | A | B | Temp |
|---|---|---|---|
| On entry to Swap (copies of X and Y) | 5 | 9 | - |
Temp ← A | 5 | 9 | 5 |
A ← B | 9 | 9 | 5 |
B ← Temp | 9 | 5 | 5 |
After the three assignments, A = 9 and B = 5, so the OUTPUT statement inside Swap displays
A = 9, B = 5. [3 marks]: [1] for the correct value of Temp (5), [1] for the correct
final values of A (9) and B (5), [1] for correctly stating the values output inside Swap.
Part (b): Why X and Y are unaffected
The final line outputs X = 5 and Y = 9, exactly the values X and Y held before the
call, not the swapped values seen inside Swap. [1 mark]
This is because BYVAL copies the value of the argument into the parameter when the procedure is
called. A and B are entirely separate variables from X and Y, local to Swap, so changing
A and B inside the procedure only changes those local copies. Once Swap finishes running,
A, B and Temp cease to exist, and X and Y in the main program were never touched. [1
mark] for correctly explaining that BYVAL operates on a copy, so the caller’s variables cannot
be modified by the procedure.
Part (c): Fixing Swap with BYREF
Changing the header to:
PROCEDURE Swap(BYREF A : INTEGER, BYREF B : INTEGER)
is the one change needed. [1 mark]
With BYREF, A and B are not copies, they are direct references to whichever variables are
supplied as arguments in the call, so inside Swap, A is X and B is Y for the
duration of the call. Any assignment to A or B is therefore an assignment to X or Y
themselves. Retracing the call with this change: Temp ← A (Temp = 5), A ← B (so X becomes
9), B ← Temp (so Y becomes 5), giving After call: X = 9, Y = 5, a genuine swap. [1 mark]
for explaining that BYREF parameters are aliases of the caller’s variables rather than copies.
Final answers
- (a)
A = 9,B = 5,Temp = 5;SwapoutputsA = 9, B = 5. - (b)
X = 5,Y = 9(unchanged), becauseBYVALparameters are copies local toSwap. - (c) Change both parameters to
BYREF, sinceBYREFparameters alias the caller’s variables directly, so the call would then outputX = 9, Y = 5.