Why use constants in a program

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

Why use constants in a program

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Why is CONSTANT VatRate ← 0.2 clearer than writing 0.2 in every calculation?
A The name VatRate tells the reader what the value means
B The name VatRate makes the program run faster
2. The VAT rate changes from 0.2 to 0.25. The program uses the constant VatRate. How many places must you edit?
A One: the CONSTANT line
B Every line that uses VatRate
3. Which line is better for a price calculation?
A Total ← Price + Price * VatRate
B Total ← Price + Price * 0.2
4. A program has the number 0.2 typed in 15 different lines. The rate changes. What is the risk?
A The program will refuse to start
B You might miss one line, so some results use the old rate
5. What is a magic number?
A A number that always equals zero
B A bare value in code with no name to explain its meaning

Match the Pairs

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

Left columnRight column
1CONSTANT VatRate ← 0.2 AGives the number 0.2 a meaningful name, so later lines can use VatRate instead of an unexplained magic number
2Total ← Price * VatRate BCONSTANT VatRate ← 0.25
3A magic number in a calculation is hard to understand, because the reader cannot tell what 0.2 stands for CThe rate 0.2 is typed as a magic number on every line. When the rate changes, every line must be edited, which is slow and easy to get wrong.
4Changing a rate once: edit only the CONSTANT line and every calculation using the name gets the new value DUses the named constant in a calculation, so if the rate changes only the CONSTANT line needs editing
5Tax1 ← Price1 * 0.2 Tax2 ← Price2 * 0.2 Tax3 ← Price3 * 0.2 EOne line was missed when the rate was updated, so the program uses two different rates and gives wrong results.
6Tax1 ← Price1 * 0.25 Tax2 ← Price2 * 0.2 Tax3 ← Price3 * 0.25 FTax ← Price * 0.2

Definitions

CONSTANT VatRate ← 0.2

Fill in the Blanks

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

1.
________ VatRate ← 0.2 Tax ← Price * VatRate
2.
CONSTANT VatRate _ 0.2 Tax ← Price * VatRate
3.
CONSTANT _______ ← 0.2 Tax ← Price * VatRate OUTPUT Tax
4.
CONSTANT VatRate ← 0.2 Tax ← Price _ VatRate
5.
CONSTANT VatRate ← 0.25 Tax1 ← Price1 * _______ Tax2 ← Price2 * VatRate

Spot the Error

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

Question 1
A DECLARE Price : REAL
B CONSTANT VatRate = 0.2
C INPUT Price
D OUTPUT Price * VatRate
Question 2
A CONSTANT PassMark ← 40
B IF Score >= 40
C THEN
D OUTPUT "Pass"
E ENDIF
Question 3
A CONSTANT VatRate ← 0.2
B INPUT Price
C Total ← Price * (1 + 0.2)
D OUTPUT Total
Question 4
A DECLARE MaxScore ← 50
B INPUT Mark
C Pct ← Mark / MaxScore * 100
D OUTPUT Pct
Question 5
A CONSTANT Rate ← 0.05
B INPUT Savings
C Interest ← Savings + Rate
D OUTPUT Interest

Answer Key

Why use constants in a program
Section A: Quick Questions
Q1
A — The name VatRate tells the reader what the value means
Q2
A — One: the CONSTANT line
Q3
A — Total ← Price + Price * VatRate
Q4
B — You might miss one line, so some results use the old rate
Q5
B — A bare value in code with no name to explain its meaning
Section B: Match the Pairs
Q1
CONSTANT VatRate ← 0.2 → Gives the number 0.2 a meaningful name, so later lines can use VatRate instead of an unexplained magic number
Q2
Total ← Price * VatRate → Uses the named constant in a calculation, so if the rate changes only the CONSTANT line needs editing
Q3
A magic number in a calculation is hard to understand, because the reader cannot tell what 0.2 stands for → Tax ← Price * 0.2
Q4
Changing a rate once: edit only the CONSTANT line and every calculation using the name gets the new value → CONSTANT VatRate ← 0.25
Q5
Tax1 ← Price1 * 0.2 Tax2 ← Price2 * 0.2 Tax3 ← Price3 * 0.2 → The rate 0.2 is typed as a magic number on every line. When the rate changes, every line must be edited, which is slow and easy to get wrong.
Q6
Tax1 ← Price1 * 0.25 Tax2 ← Price2 * 0.2 Tax3 ← Price3 * 0.25 → One line was missed when the rate was updated, so the program uses two different rates and gives wrong results.
Section C: Definitions
Q1
CONSTANT VatRate ← 0.2 — A named value used instead of a magic number. It makes code easier to read, and when the value changes you edit it in one place only, so no missed edits cause mistakes.
Section D: Fill in the Blanks
Q1
CONSTANT VatRate ← 0.2 Tax ← Price * VatRate
Q2
CONSTANT VatRate ← 0.2 Tax ← Price * VatRate
Q3
CONSTANT VatRate ← 0.2 Tax ← Price * VatRate OUTPUT Tax
Q4
CONSTANT VatRate ← 0.2 Tax ← Price * VatRate
Q5
CONSTANT VatRate ← 0.25 Tax1 ← Price1 * VatRate Tax2 ← Price2 * VatRate
Section E: Spot the Error
Q1
B — A constant is given its value with the ← operator, not =. It should be CONSTANT VatRate ← 0.2, so the named value is set in one place.
Q2
B — 40 is a magic number here. The check should use the constant PassMark, so if the pass mark changes it only needs editing on line 1.
Q3
C — The rate 0.2 is typed directly instead of using VatRate. If the VAT rate changes, this line could be missed. It should use VatRate.
Q4
A — MaxScore never changes, so it should be declared with CONSTANT, not DECLARE. A constant keeps the fixed value in one named place.
Q5
C — Interest is found by multiplying the savings by the rate, not adding. It should be Savings * Rate, using the constant Rate.