AS Level · Topic 11.3
A-Level Computer Science 9618 / Topic 11 / AS

A copy or the original?

When a variable is passed to a procedure, the procedure gets either a copy of its value or access to the variable itself. That one choice decides whether the caller's data can change. Learn BYVAL and BYREF, trace calls that use both, and know when each is the right choice.

The key idea

Passing by value (BYVAL) gives the subroutine a copy of the argument's value: changes to the parameter are local and the caller's variable is unchanged. Passing by reference (BYREF) passes the address of the caller's variable, so the parameter is another name for the same memory location and any change is made to the original. If neither keyword is given, passing by value is assumed. In 9618 pseudocode, parameters should not be passed by reference to a function.

BYVAL: a copy BYREF: the original callerprocedurecallerprocedure X = 5 A = 5 value copied A ← A * 2 A becomes 10, X stays 5 Y = 3 B address passed B ← B + 10 B is another name for Y: Y becomes 13
Fig. 1: BYVAL copies the value into a new location; BYREF lets the parameter refer to the caller's own variable
Section 01

Passing by value.

With BYVAL the procedure receives a copy of the value. It can use and change its copy freely, but the variable in the calling code keeps its value. This protects the caller's data, and the argument can even be a literal or an expression.

PROCEDURE Double(BYVAL N : INTEGER)
    N ← N * 2
    OUTPUT "Inside: ", N
ENDPROCEDURE

DECLARE Score : INTEGER
Score ← 7
CALL Double(Score)
OUTPUT "Outside: ", Score
Worked example Trace a BYVAL call
Trace the program above and give both lines of output.
  1. Score ← 7 in the main program.
  2. CALL Double(Score): the value 7 is copied into the parameter N.
  3. N ← 7 × 2 = 14 and "Inside: 14" is output. Only the copy changed.
  4. The procedure ends, N is discarded, and "Outside: 7" is output.
Answer:
ScoreNOUTPUT
7
7
14Inside: 14
7Outside: 7
Your turn
What is output by CALL Double(Score + 1) when Score is 7, and what is Score afterwards?
Examiner trap

"The procedure changes N, so Score changes" is the classic wrong answer. With BYVAL the procedure only ever touches its own copy.

Section 02

Passing by reference.

With BYREF the address of the caller's variable is passed, so the parameter and the variable are the same memory location under two names. Every change inside the procedure is a change to the original. Run the lab's two Swap programs, one with BYREF and one with BYVAL, and compare the outputs.

PROCEDURE Swap(BYREF X : INTEGER, Y : INTEGER)
    DECLARE Temp : INTEGER
    Temp ← X
    X ← Y
    Y ← Temp
ENDPROCEDURE

DECLARE A : INTEGER
DECLARE B : INTEGER
A ← 3
B ← 8
CALL Swap(A, B)
OUTPUT A, " ", B
Worked example Trace the swap
Trace the Swap program and give the output. Then give the output if BYREF were changed to BYVAL.
  1. A ← 3, B ← 8. In the call, X refers to A and Y refers to B.
  2. Temp ← X, so Temp = 3.
  3. X ← Y sets A to 8; Y ← Temp sets B to 3.
  4. Output: 8 3. With BYVAL, X and Y are copies that are swapped and then discarded, so the output is 3 8.
Answer:
A (X)B (Y)TempOUTPUT
38
3
8
38 3
BYREF: 8 3. BYVAL: 3 8.
Your turn
PROCEDURE Inc(BYREF N : INTEGER) contains only N ← N + 1. Count is 0. What is Count after CALL Inc(Count) is executed twice?
Examiner trap

In PROCEDURE Swap(BYREF X : INTEGER, Y : INTEGER), Y is passed by reference too. Saying Y is passed by value loses the mark.

Stage 1 · Learn

Check what the sim just showed you

Four quick checks. Each correct answer earns XP and records this skill on your profile.

Quick check+10 XP

If a procedure header does not say BYVAL or BYREF, how are its parameters passed?

Quick check+10 XP

In PROCEDURE Swap(BYREF X : INTEGER, Y : INTEGER), how is Y passed?

Quick check+10 XP

In the lab, A is 3 and B is 8. What does the BYVAL version of Swap output?

Quick check+10 XP

What is actually passed to a procedure when a parameter is passed by reference?

Section 03

Choosing the method.

Pass by value unless the procedure has a good reason to change the caller's variable. A procedure that must hand back more than one result uses BYREF parameters for them; inputs that it only reads stay BYVAL.

PROCEDURE Split(BYVAL TotalMins : INTEGER, BYREF Hours : INTEGER, Mins : INTEGER)
    Hours ← TotalMins DIV 60
    Mins ← TotalMins MOD 60
ENDPROCEDURE

CALL Split(135, H, M)
OUTPUT H, " h ", M, " min"
// outputs 2 h 15 min
Use BYVAL whenUse BYREF when
the procedure only needs to read the valuethe procedure must change the caller's variable, such as Swap
the caller's data must be protected from accidental changea procedure must pass back more than one value, such as Split
the argument is a literal or an expression(in real languages) copying a very large structure would waste time and memory
Worked example Mixed parameters
Give the output of this program (see Fig. 1).
PROCEDURE Adjust(BYVAL A : INTEGER, BYREF B : INTEGER), whose body is A ← A * 2 then B ← B + A. The main program sets X ← 5 and Y ← 3, runs CALL Adjust(X, Y), then OUTPUT X, " ", Y.
  1. A receives a copy of X, so A = 5. B refers to Y, which is 3.
  2. A ← 5 × 2 = 10. Only the copy changes; X is still 5.
  3. B ← 3 + 10 = 13. B is Y, so Y becomes 13.
  4. Output: 5 13.
Answer: 5 13
Your turn
Explain why CALL Split(135, H, M) works but CALL Split(H, 135, M) cannot.
Examiner trap

If a question says a procedure must "return" two values, the answer is BYREF parameters, not two RETURN statements: a procedure has no RETURN value, and a function returns only one value.

Stage 2 · Exam

Exam-style questions

Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.

Finish the four checks above to unlock the exam questions
Exam style+20 XP

PROCEDURE Inc(BYREF N : INTEGER) contains N ← N + 1. Count ← 0, then CALL Inc(Count) runs three times. What is Count?

Exam style+20 XP

Why is CALL Swap(A, 5) invalid when both parameters of Swap are BYREF?

Exam style+20 XP

Which header follows the 9618 pseudocode guide?

Exam style+20 XP

Adjust(BYVAL A : INTEGER, BYREF B : INTEGER) does A ← A * 2 then B ← B + A. X is 5 and Y is 3. After CALL Adjust(X, Y), what are X and Y?

Skill unlocked

Passing parameters by value and by reference, mastered.

This skill is now recorded on your profile. Keep the chain going.

-Rank -Level -Score -Topics
Go deeper · practice
Six original Cambridge-style questions
Passing parameters BYVAL and BYREF, tracing procedure calls that change or protect the caller's variables, and choosing the right method.