← IGCSE Computer Science Paper 2

Changing code in one place

Code kept in one subroutine is edited once, so every CALL gets the update. Repeated copies must each be changed, and missing one gives inconsistent results.

Understanding Changing code in one place in IGCSE Computer Science Paper 2

What it does: Every call runs the same ShowScore code, so correcting the bug inside ShowScore corrects every call. One line was updated to 1.20 but the other still uses 1.15, so the program gives different results for the same rule. The rate is stored in one place, the function. Every CALL uses the updated function, so you only edit it once. Repeated copies are separate pieces of code. Each one must be edited by hand. The benefit is easier maintenance: less editing and fewer inconsistent results. It is not about speed. Mixing a call with a copied calculation means the copy is missed when the function is changed. Both lines should call AddTax.

Common mistakes: A function returns a value, so it is used inside an expression or OUTPUT, not with CALL. It should be OUTPUT Tax(50). Once the rate is changed inside Tax, this call gets the update. A function must RETURN its result so every call can use it. It should be RETURN P * 0.25. The rate is changed once here and both calls get the new value.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 439–448) and word search (Modules 439–458).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
A shop calculates tax in a function called CalculateTax. The tax rate changes. Where should you make the change?
In every line that calls CalculateTaxOnce, inside the CalculateTax function
Matches
Cost1 ← Price1 * 1.15 Cost2 ← Price2 * 1.15 Cost3 ← Price3 * 1.15
↓
The tax calculation is copied into three places, so a rate change needs three edits. Put it in one function and call it instead.
Applying
Meanings
Term
single point of change
Meaning
Code kept in one subroutine is edited once, so every CALL gets the update. Repeated copies must each be changed, and missing one gives inconsistent results.
Practice
// Tax rate changed here only
Testing
Gaps
Price * 1.2
Check Answer
Spots
3ENDFUNCTION
4CALL Tax(50)

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Penguin' badge — List Waddler, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. A shop calculates tax in a function called CalculateTax. The tax rate changes. Where should you make the change?
A. In every line that calls CalculateTax
B. Once, inside the CalculateTax function ✓
The rate is stored in one place, the function. Every CALL uses the updated function, so you only edit it once.
2. A program has the same tax calculation typed out 6 times, with no subroutine. The rate changes. What must you do?
A. Change just one copy and the rest update
B. Change all 6 copies of the code ✓
Repeated copies are separate pieces of code. Each one must be edited by hand.
3. Which statement about a bug inside a subroutine that is called 10 times is correct?
A. Fix the bug once in the subroutine and all 10 calls are fixed ✓
B. Fix the bug in each of the 10 CALL lines separately
The CALL lines only run the subroutine. The code that holds the bug is in the subroutine, so one fix covers every call.
4. A programmer updates 5 of 6 copies of repeated tax code and misses one. What is the likely result?
A. Inconsistent results: one place uses the old rate ✓
B. All six places automatically use the new rate
The missed copy still has the old rate, so the program gives different answers for the same job.
5. Which program design makes a future change to the tax rate less likely to cause mistakes?
A. FUNCTION CalculateTax called wherever tax is needed ✓
B. Typing price * 0.2 again wherever tax is needed
One function means one place to edit. Typing the calculation again and again creates many copies to keep in step.

Match the Pairs

FUNCTION AddTax(Price : REAL) RETURNS REAL RETURN Price * 1.15 ENDFUNCTION → The tax rate is written in one function, so changing 1.15 here updates every call to AddTax at once.
Total1 ← Price1 * 1.15 Total2 ← Price2 * 1.15 Total3 ← Price3 * 1.15 → The same calculation is copied three times, so a new tax rate must be changed in every copy.
A bug fixed once inside a subroutine is fixed for every place that calls it → CALL ShowScore(Score)
Missing one copy of repeated code after a change gives inconsistent results → Total1 ← Price1 * 1.20 Total2 ← Price2 * 1.15
Cost1 ← Price1 * 1.15 Cost2 ← Price2 * 1.15 Cost3 ← Price3 * 1.15 → The tax calculation is copied into three places, so a rate change needs three edits. Put it in one function and call it instead.
OUTPUT AddTax(Price1) OUTPUT Price2 * 1.15 → One line calls AddTax but the other copies the calculation. If the rate in AddTax changes, the second line will not update and the results will be inconsistent. Use AddTax for both.

Key Term

single point of change
Code kept in one subroutine is edited once, so every CALL gets the update. Repeated copies must each be changed, and missing one gives inconsistent results.

Fill in the Blanks

1. FUNCTION AddTax(Price : REAL) RETURNS REAL RETURN Price * 1.2 ENDFUNCTION
Hint: Type the keyword that sends the result back to the caller. Changing the rate here updates every call.
2. OUTPUT AddTax(Cost1) OUTPUT AddTax(Cost2) OUTPUT AddTax(Cost3)
Hint: Type the function name. Every call to it gets the new tax rate when the function is edited once.
3. PROCEDURE ShowPrice(Cost : REAL) OUTPUT AddTax(Cost) ENDPROCEDURE
Hint: Type the keyword that closes the procedure, so the code to edit stays in one place.
4. OUTPUT Price1 * 1.25 OUTPUT Price2 * 1.25 OUTPUT Price3 * 1.25
Hint: Type the rate that matches the other copies. If one copy is missed, the results are inconsistent.
5. FUNCTION IsAdult(Age : INTEGER) RETURNS BOOLEAN RETURN Age >= 18 ENDFUNCTION
Hint: Type the comparison operator meaning 'greater than or equal to'. Fixing this bug once fixes every call.

Spot the Error

Question 1
A FUNCTION Tax(P) RETURNS REAL
B RETURN P * 0.2
C ENDFUNCTION
D CALL Tax(50)
Line D — A function returns a value, so it is used inside an expression or OUTPUT, not with CALL. It should be OUTPUT Tax(50). Once the rate is changed inside Tax, this call gets the update.
Question 2
A FUNCTION Tax(P) RETURNS REAL
B OUTPUT P * 0.25
C ENDFUNCTION
D OUTPUT Tax(20)
E OUTPUT Tax(80)
Line B — A function must RETURN its result so every call can use it. It should be RETURN P * 0.25. The rate is changed once here and both calls get the new value.
Question 3
A CONSTANT TaxRate ← 0.2
B FUNCTION Gross(P) RETURNS REAL
C RETURN P + P * 0.2
D ENDFUNCTION
E OUTPUT Gross(10)
Line C — The rate is typed in again instead of using TaxRate. If the constant is changed, this copy is missed and gives inconsistent results. It should be RETURN P + P * TaxRate.
Question 4
A FUNCTION Double(N) RETURNS INTEGER
B RETURN N * 2
C ENDFUNCTION
D Total ← Double(5) + Double 7
Line D — Every call needs brackets around its argument, so Double 7 is invalid. It should be Double(7). Both calls then get any fix made inside Double.
Question 5
A PROCEDURE ShowPrice(Cost)
B Price ← ROUND(Cost * 1.2)
C OUTPUT Price
D ENDPROCEDURE
Line B — ROUND needs a second argument for the number of decimal places. It should be ROUND(Cost * 1.2, 2). The fix is made once and every call of ShowPrice gets it.

Student with a class code? Join your class →