Copy or original.

Six original Cambridge-style questions on passing parameters by value and by reference, with mark points.

Original questions All questions on this page are original work, written in the Cambridge AS & A Level style. They are not from past papers. They test the same concepts and skills the syllabus rewards.
Keep these straight

Value in, reference out.

01
Recall
[2 marks]

Explain the difference between passing a parameter by value and by reference.

  • By value: a copy of the value is passed; changes made in the subroutine do not affect the original variable. ✓
  • By reference: the address of the variable is passed; changes made in the subroutine are made to the original variable. ✓
02
Trace
[4 marks]

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, " ", Y
  • First call: A = 4 (copy of X), B refers to Y; A ← 5, B ← 10, so Y = 10 and X is unchanged. ✓
  • First output: 4 10. ✓
  • Second call: A = 10 (copy of Y), B refers to X (4); A ← 14, B ← 28, so X = 28. ✓
  • Second output: 28 10. ✓
03
Write
[4 marks]

Write 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.

  • Header: PROCEDURE Order(BYREF First : INTEGER, Second : INTEGER) (both by reference). ✓
  • IF First > Second THEN ... ENDIF. ✓
  • Swap using a local temporary variable: Temp ← First, First ← Second, Second ← Temp. ✓
  • ENDPROCEDURE, with Temp declared as INTEGER. ✓
04
Write
[5 marks]

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. ✓
  • Output: 21 7.0 (Mean is REAL). ✓
05
Explain
[2 marks]

Explain why a procedure, rather than a function, is used when a subroutine must pass back two values to the calling program.

  • A function returns only a single value (and should not use BYREF parameters). ✓
  • A procedure can pass back several values through BYREF parameters, which update the caller's variables. ✓
06
Analysis
[3 marks]

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.

  • No keyword is given, so Score is passed by value: the procedure adds 10 to a copy, which is discarded. ✓
  • Fix 1: change the header to PROCEDURE AddBonus(BYREF Score : INTEGER) so the original variable is updated. ✓
  • Fix 2: make it a function, 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.