Six original Cambridge-style questions on passing parameters by value and by reference, with mark points.
Explain the difference between passing a parameter by value and by reference.
Give the two lines of output from this program.
PROCEDURE Process(BYVAL A : INTEGER, BYREF B : INTEGER)
A ← A + B
B ← A * 2
ENDPROCEDURE
X ← 4
Y ← 1
CALL Process(X, Y)
OUTPUT X, " ", Y
CALL Process(Y, X)
OUTPUT X, " ", YWrite a procedure Order with two integer parameters that, after it is called, leaves the smaller value in the first variable and the larger value in the second.
PROCEDURE Order(BYREF First : INTEGER, Second : INTEGER) (both by reference). ✓IF First > Second THEN ... ENDIF. ✓Temp ← First, First ← Second, Second ← Temp. ✓ENDPROCEDURE, with Temp declared as INTEGER. ✓Write a procedure Stats that takes three integers and passes back both their total and their mean. Write a call that passes 4, 9 and 8 and then outputs the results, and state what is output.
PROCEDURE Stats(BYVAL N1 : INTEGER, N2 : INTEGER, N3 : INTEGER, BYREF Total : INTEGER, Mean : REAL). ✓Total ← N1 + N2 + N3. ✓Mean ← Total / 3 and ENDPROCEDURE. ✓CALL Stats(4, 9, 8, Sum, Average) then OUTPUT Sum, " ", Average, with Sum and Average declared as variables. ✓Explain why a procedure, rather than a function, is used when a subroutine must pass back two values to the calling program.
A student writes PROCEDURE AddBonus(Score : INTEGER) containing Score ← Score + 10. After CALL AddBonus(PlayerScore), PlayerScore has not changed. Explain why, and describe two different ways to correct it.
PROCEDURE AddBonus(BYREF Score : INTEGER) so the original variable is updated. ✓FUNCTION AddBonus(Score : INTEGER) RETURNS INTEGER that returns Score + 10, and call it with PlayerScore ← AddBonus(PlayerScore). ✓Mark this once you have attempted all six and checked your working. It records a Practiced badge on the topic and adds a one-time bonus. Revealing the solutions alone does not count.