The shape of the lesson
By the end of the lesson, learners can
- 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.
- 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.
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.
Forty-five minutes, phase by phase
| Time | Phase | What happens in the room | Grouping |
|---|---|---|---|
| 0 to 5 min | Hook: predict | Open 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 min | Teach and check: Passing by value | With 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 min | Teach and check: 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. 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 min | Teach and check: Choosing the method | Pass 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 min | Check | The 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 min | Plenary | The 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 min | Exit and homework | Set the worksheet below (6 questions, 20 marks). Learners self-assess against the objectives before they leave. | Individual |
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.
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.
What to head off, and how
| Trap learners fall into | Teaching 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. |
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.
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.
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.
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.
Answer key and marking notes
- 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.
- 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.
- 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.
PROCEDURE Stats(BYVAL N1 : INTEGER, N2 : INTEGER, N3 : INTEGER, BYREF Total : INTEGER, Mean : REAL).Total ← N1 + N2 + N3.Mean ← Total / 3andENDPROCEDURE.CALL Stats(4, 9, 8, Sum, Average)thenOUTPUT Sum, " ", Average, with Sum and Average declared as variables.- Output: 21 7.0 (Mean is REAL).
- 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.
- 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 INTEGERthat returns Score + 10, and call it withPlayerScore ← AddBonus(PlayerScore).
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.
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
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
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
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
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
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
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.
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.
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.
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.
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.
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.
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 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.