Changing code in one place

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Changing code in one place

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1FUNCTION AddTax(Price : REAL) RETURNS REAL RETURN Price * 1.15 ENDFUNCTION AThe tax calculation is copied into three places, so a rate change needs three edits. Put it in one function and call it instead.
2Total1 ← Price1 * 1.15 Total2 ← Price2 * 1.15 Total3 ← Price3 * 1.15 BCALL ShowScore(Score)
3A bug fixed once inside a subroutine is fixed for every place that calls it CThe same calculation is copied three times, so a new tax rate must be changed in every copy.
4Missing one copy of repeated code after a change gives inconsistent results DOne 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.
5Cost1 ← Price1 * 1.15 Cost2 ← Price2 * 1.15 Cost3 ← Price3 * 1.15 EThe tax rate is written in one function, so changing 1.15 here updates every call to AddTax at once.
6OUTPUT AddTax(Price1) OUTPUT Price2 * 1.15 FTotal1 ← Price1 * 1.20 Total2 ← Price2 * 1.15

Definitions

single point of change

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
FUNCTION AddTax(Price : REAL) RETURNS REAL ______ Price * 1.2 ENDFUNCTION
2.
OUTPUT AddTax(Cost1) OUTPUT ______(Cost2) OUTPUT AddTax(Cost3)
3.
PROCEDURE ShowPrice(Cost : REAL) OUTPUT AddTax(Cost) ____________
4.
OUTPUT Price1 * 1.25 OUTPUT Price2 * ____ OUTPUT Price3 * 1.25
5.
FUNCTION IsAdult(Age : INTEGER) RETURNS BOOLEAN RETURN Age __ 18 ENDFUNCTION

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A FUNCTION Tax(P) RETURNS REAL
B RETURN P * 0.2
C ENDFUNCTION
D CALL Tax(50)
Question 2
A FUNCTION Tax(P) RETURNS REAL
B OUTPUT P * 0.25
C ENDFUNCTION
D OUTPUT Tax(20)
E OUTPUT Tax(80)
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)
Question 4
A FUNCTION Double(N) RETURNS INTEGER
B RETURN N * 2
C ENDFUNCTION
D Total ← Double(5) + Double 7
Question 5
A PROCEDURE ShowPrice(Cost)
B Price ← ROUND(Cost * 1.2)
C OUTPUT Price
D ENDPROCEDURE

Answer Key

Changing code in one place
Section A: Quick Questions
Q1
B — Once, inside the CalculateTax function
Q2
B — Change all 6 copies of the code
Q3
A — Fix the bug once in the subroutine and all 10 calls are fixed
Q4
A — Inconsistent results: one place uses the old rate
Q5
A — FUNCTION CalculateTax called wherever tax is needed
Section B: Match the Pairs
Q1
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.
Q2
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.
Q3
A bug fixed once inside a subroutine is fixed for every place that calls it → CALL ShowScore(Score)
Q4
Missing one copy of repeated code after a change gives inconsistent results → Total1 ← Price1 * 1.20 Total2 ← Price2 * 1.15
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
FUNCTION AddTax(Price : REAL) RETURNS REAL RETURN Price * 1.2 ENDFUNCTION
Q2
OUTPUT AddTax(Cost1) OUTPUT AddTax(Cost2) OUTPUT AddTax(Cost3)
Q3
PROCEDURE ShowPrice(Cost : REAL) OUTPUT AddTax(Cost) ENDPROCEDURE
Q4
OUTPUT Price1 * 1.25 OUTPUT Price2 * 1.25 OUTPUT Price3 * 1.25
Q5
FUNCTION IsAdult(Age : INTEGER) RETURNS BOOLEAN RETURN Age >= 18 ENDFUNCTION
Section E: Spot the Error
Q1
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.
Q2
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.
Q3
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.
Q4
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.
Q5
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.