Loading...
Trace the relationship between a formal parameter and the caller’s variable. OCR’s explicit byVal/byRef model is distinct from Python’s binding of names to objects.
What you'll be able to do
A by-value parameter receives a value whose local reassignment does not change the caller’s variable through that parameter. In a conventional by-reference model, the formal parameter refers to the caller’s storage, so assigning through it changes that variable. OCR assumes by value unless a question states otherwise; relevant explicit cases use byVal or byRef.
Formal parameters are names in the routine definition; actual arguments are supplied in the call. For increase(a:byVal,b:byRef), passing x and y creates different relationships even if x and y initially contain the same integer. Trace bindings and storage separately instead of deciding the mode from whether a variable name is short or global.
In the example, x=4 and y=4. The local by-value a becomes 5, while the caller’s x remains 4. Assigning b=b+1 updates the referenced y to 5. The output after the call is therefore 4,5. The routine’s local names a and b do not themselves become global names.
Scope describes where a name can be used; parameter mode describes how an argument is associated with a parameter. A locally scoped reference parameter can still modify external storage. Conversely, a by-value parameter’s temporary local reassignment need not affect the caller. Returning a result is another explicit communication mechanism and can make a calculation easier to test without hidden mutation.
Python binds a parameter name to the supplied object. Rebinding that local name does not rebind the caller’s name, while mutating a shared mutable object can be observed by the caller. This is not a traditional general byRef parameter for every argument, and not a deep copy of every passed object. Apply Python’s actual rules to code labelled Python.
Reference-style updates can suit an intended output parameter or shared structure, but can also make side effects and aliasing harder to reason about. Passing or returning values can make dependencies clearer, with copying and object-sharing costs depending on the language and representation. In an exam, use the question’s model; in program design, document what can change and test the caller-visible result.
procedure increase(a:byVal, b:byRef)
a = a + 1
b = b + 1
endprocedure
x = 4
y = 4
increase(x, y)
print(x)
print(y)Scroll horizontally to see the full code or table.| Storage/name | Before call | After relevant assignment | After return |
|---|---|---|---|
| Caller x | 4 | Unchanged by a assignment | 4 |
| Local a | Receives 4 | 5 | Local call ended |
| Caller y through b | 4 | 5 | 5 |
Attempt the task before opening its worked response. Record each change of state, explain the rule used, and check the result against the original requirements. Code examples labelled Python 3 use that language’s conventions; pseudocode states its own assumptions.
Common mistakes to avoid
Key takeaways
Test yourself
Answer a few questions, then review anything you missed.
Start with the quiz above to check the key ideas. You can also choose another topic from the practice library.