Teachers' Portal  /  A-Level Computer Science 9618  /  Programming  /  Passing parameters by value and by reference
Lesson pack · A-Level Computer Science 9618 · 11.3 · AS Level

Passing parameters by value and by reference

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.

Student lesson Lab, full screen
At a glance

The shape of the lesson

Topic
Programming
Syllabus reference
Cambridge International AS and A Level Computer Science 9618, 11.3
Level
AS Level
Duration
45 minutes, lab-led; stretches to a 60 minute block (see the contingency notes)
Position in topic
Lesson 4 of 4 in Programming; follows Procedures and functions
Central model
Fig. 1: BYVAL copies the value into a new location; BYREF lets the parameter refer to the caller's own variable
Cooperative structure
Assessment
Stage 1 check and the exam-style plenary from the student lesson, plus the worksheet as the exit task
Learning objectives

By the end of the lesson, learners can

Section by section
  • Passing by value: With BYVAL the procedure receives a copy of the value.
  • 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.
  • Choosing the method: Pass by value unless the procedure has a good reason to change the caller's variable.
Key points to leave on the board
  • BYVAL: a copy; changes stay inside the subroutine; the default if nothing is stated.
  • BYREF: the address; the parameter is the caller's variable; the argument must be a variable.
  • One keyword applies to all the following parameters until the other keyword appears.
  • Functions take BYVAL parameters only and return one value; use BYREF in a procedure to pass back several values.
The core visual model

One picture every idea returns to

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
Lesson sequence

Forty-five minutes, phase by phase

TimePhaseWhat happens in the roomGrouping
0 to 5 minHook: predictOpen with the question in the title, 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. Put the figure on the board: it is the one picture every idea in the lesson returns to.Whole class, then pairs
5 to 13 minTeach and check: Passing by valueWith BYVAL the procedure receives a copy of the value. Model the worked example Trace a BYVAL call on the board, one line at a time. Your turn: What is output by CALL Double(Score + 1) when Score is 7, and what is Score afterwards?Whole class, then pairs
13 to 21 minTeach and check: Passing by referenceWith BYREF the address of the caller's variable is passed, so the parameter and the variable are the same memory location under two names. Project tab 7 of the lab (9618 pseudocode: BYREF and BYVAL) and let a student drive it while the class predicts. Model the worked example Trace the swap on the board, one line at a time. 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?Pairs
21 to 30 minTeach and check: Choosing the methodPass by value unless the procedure has a good reason to change the caller's variable. Model the worked example Mixed parameters on the board, one line at a time. Your turn: Explain why CALL Split(135, H, M) works but CALL Split(H, 135, M) cannot.Jigsaw
30 to 36 minCheckThe four Stage 1 questions from the student lesson, answered on mini whiteboards or by show of fingers. Re-teach the one most get wrong.Individual, then whole class
36 to 43 minPlenaryThe exam-style questions from Stage 2, one at a time: thirty seconds alone, then a random call. The feedback lines in the key give the examiner's wording.Individual, random call
43 to 45 minExit and homeworkSet the worksheet below (6 questions, 20 marks). Learners self-assess against the objectives before they leave.Individual
Timing and contingency

Protect the parts that carry the learning

The sequence carries about fifty minutes of material, so the lab is driven by one student while the class predicts rather than explored freely. Times are a guide.

Protected: the check at 30 minutes. It is the only individual read on whether the key idea landed, and it is not cut.

Cut first if the build overruns: the last check in Choosing the method, never the check or the plenary.

In a 60 minute block: run worksheet questions 1 to 3 in class as a Rally Coach set and model both the hardest worked example and its check live.

Running the cooperative task

Jigsaw

Split the content into expert pieces. Expert groups master one piece each, then regroup so every home team has one expert per piece, and each expert teaches their piece to the team.

The full structure, with its variations and the other twelve, is on the cooperative-learning page.

Examiner traps to pre-empt

What to head off, and how

Trap learners fall intoTeaching move that pre-empts it
"The procedure changes N, so Score changes" is the classic wrong answer.With BYVAL the procedure only ever touches its own copy.
In PROCEDURE Swap(BYREF X: INTEGER, Y: INTEGER), Y is passed by reference too.Saying Y is passed by value loses the mark.
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.Return to Choosing the method: ask a pair to state the correct version and show it on the lab before moving on.
Worksheet

Passing parameters by value and by reference: worksheet

6 questions, 20 marks. Written in the style of the papers; no past paper question is reproduced.

NameClassDate
Q1 · recall[2 marks]

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

Q2 · 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
Q3 · 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.

Q4 · 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.

Q5 · 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.

Q6 · 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.

Answer key and marking notes
Q1 · 2 marks
  • 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.
Q2 · 4 marks
  • 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.
Q3 · 4 marks
  • 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.
Q4 · 5 marks
  • 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).
Q5 · 2 marks
  • 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.
Q6 · 3 marks
  • 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).
Quiz

6 multiple-choice questions on this lesson

From the topic question bank. Assign them in the app for a marked score in your gradebook, or run the topic as a class game.

Q11 mark

Which statement about passing a parameter by value is correct?

  • AThe subroutine works on a copy, so the caller's variable keeps its original value
  • BThe address of the caller's variable is passed, so changes are made to the original
  • CThe argument must be a variable, because a literal has no memory location to copy
  • DIt must be stated with BYVAL, because BYREF is assumed when no keyword is given
Q21 mark

What is output?

PROCEDURE Triple(BYREF N : INTEGER)
    N ← N * 3
ENDPROCEDURE

X ← 2
CALL Triple(X)
CALL Triple(X)
OUTPUT X

  • A18
  • B2
  • C6
  • D9
Q31 mark

Look at this header: PROCEDURE Update(BYVAL A : INTEGER, B : INTEGER, BYREF C : INTEGER, D : INTEGER). Which parameters are passed by reference?

  • AC and D
  • BC only
  • CA and C
  • DA, B, C and D
Q41 mark

A subroutine must find both the largest and the smallest value in an array and pass both back to the calling program. How should it be written in 9618 pseudocode?

  • AAs a procedure with two BYREF parameters that receive the largest and smallest values
  • BAs a function with two RETURN statements, one for each of the two values
  • CAs a function with two BYREF parameters, as well as a RETURNS data type
  • DAs a procedure with two BYVAL parameters that receive the largest and smallest values
Q51 mark

What is output by the last line?

PROCEDURE Mix(BYVAL P : INTEGER, BYREF Q : INTEGER)
    P ← P + Q
    Q ← P - 1
ENDPROCEDURE

A ← 6
B ← 2
CALL Mix(A, B)
CALL Mix(B, A)
OUTPUT A, " ", B

  • A12 7
  • B6 2
  • C14 15
  • D6 7
Q61 mark

A procedure has the header PROCEDURE Calc(BYREF Total : INTEGER, BYVAL Bonus : INTEGER). A student writes CALL Calc(50, Score). Why is this call wrong?

  • AThe literal 50 is matched to Total, which is BYREF and needs a variable that can be updated
  • BScore is a variable, and a variable cannot be passed to a parameter that is BYVAL
  • CBYVAL cannot come after BYREF in the same procedure header
  • DA procedure cannot have one BYREF and one BYVAL parameter at the same time
Quiz key
Q1: A

BYVAL copies the argument's value into a new location, so changes to the parameter are local. Passing the address is by reference. With BYVAL the argument can be a literal or an expression, since only its value is copied. When no keyword is given, by value is the default, not by reference.

Q2: A

Each call multiplies the original X by 3: 2 becomes 6, then 6 becomes 18. Output 2 treats the parameter as BYVAL. 6 counts only one call. 9 comes from multiplying by 3 and then adding 3 instead of multiplying the updated X by 3 again.

Q3: A

BYVAL covers A and B; BYREF then covers C and every following parameter, so C and D are by reference. "C only" forgets that the keyword carries on to D. A is explicitly BYVAL. If all four were by reference, the first keyword would have had to be BYREF.

Q4: A

BYREF parameters let a procedure update the caller's own variables, so both results come back. A function returns a single value, and the first RETURN that runs ends it, so a second RETURN would never run. The guide says parameters should not be passed by reference to a function. BYVAL parameters are copies, so values assigned to them are lost when the procedure ends.

Q5: A

Call 1: P is a copy of A (6) and Q is B. P ← 8, then B ← 7; A stays 6. Call 2: P is a copy of B (7) and Q is A. P ← 7 + 6 = 13, then A ← 12; B stays 7. Output: 12 7. "6 2" treats both parameters as BYVAL. "14 15" treats both as BYREF. "6 7" stops after the first call.

Q6: A

The first argument goes to the first parameter, so the literal 50 is passed to Total, which is BYREF: a literal has no memory location to update. The call should be something like CALL Calc(Score, 50). A variable can be passed by value; its value is simply copied. The keywords may be mixed and may appear in either order, as long as each is placed before the parameters it applies to.

Lab and demo

What to project

The student lesson embeds these tabs. Open them full screen on the projector; every slider has a drag handle on the stage.

Slides

Slides for this lesson

A projectable deck for every pack is on the way. Until then, present the student lesson full screen: its sections, figure and lab tabs follow the same order as the plan above.

Also for this topic

Everything else you can use

Original work by the TheLucidSTEM team. The worksheet items are written in the style of the papers; no past paper question is reproduced.
Previous lessonProcedures and functions