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.
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.
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
PROCEDURE Double(N : INTEGER) also passes by value, because no keyword means BYVAL.CALL Double(Score + 1).| Score | N | OUTPUT |
|---|---|---|
| 7 | ||
| 7 | ||
| 14 | Inside: 14 | |
| 7 | Outside: 7 |
CALL Double(Score + 1) when Score is 7, and what is Score afterwards?"The procedure changes N, so Score changes" is the classic wrong answer. With BYVAL the procedure only ever touches its own copy.
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
CALL Swap(A, 5) makes no sense, because a literal has no memory location to update.| A (X) | B (Y) | Temp | OUTPUT |
|---|---|---|---|
| 3 | 8 | ||
| 3 | |||
| 8 | |||
| 3 | 8 3 |
PROCEDURE Inc(BYREF N : INTEGER) contains only N ← N + 1. Count is 0. What is Count after CALL Inc(Count) is executed twice?In PROCEDURE Swap(BYREF X : INTEGER, Y : INTEGER), Y is passed by reference too. Saying Y is passed by value loses the mark.
Four quick checks. Each correct answer earns XP and records this skill on your profile.
If a procedure header does not say BYVAL or BYREF, how are its parameters passed?
In PROCEDURE Swap(BYREF X : INTEGER, Y : INTEGER), how is Y passed?
In the lab, A is 3 and B is 8. What does the BYVAL version of Swap output?
What is actually passed to a procedure when a parameter is passed by reference?
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 when | Use BYREF when |
|---|---|
| the procedure only needs to read the value | the procedure must change the caller's variable, such as Swap |
| the caller's data must be protected from accidental change | a 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 |
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.CALL Split(135, H, M) works but CALL Split(H, 135, M) cannot.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.
Unlocks once the four checks above are done. Worth more XP, written to AS Paper 1 and Paper 2 standard.
PROCEDURE Inc(BYREF N : INTEGER) contains N ← N + 1. Count ← 0, then CALL Inc(Count) runs three times. What is Count?
Why is CALL Swap(A, 5) invalid when both parameters of Swap are BYREF?
Which header follows the 9618 pseudocode guide?
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?
This skill is now recorded on your profile. Keep the chain going.