Constants and variables in a programming language — AQA A-Level Computer Science
In AQA A-Level Computer Science, variables and constants are named memory locations storing data, distinguished by whether their values can change during runtime. Using constants over hardcoded literals enhances code maintainability, reduces accidental overwriting, and enables compiler optimisation.
Constants and variables in a programming language explained
A variable is a named storage location whose value can be changed while a program runs.
Read the full explanation
A constant is a named storage location whose value is fixed when it is declared and cannot be altered afterwards. Both are identified by a name, both hold a value of a particular data type, and both are read in the same way within an expression. The key difference is mutability: an assignment such as total = total + price is valid for a variable but would be rejected or ignored for a constant such as VAT_RATE = 0.2. A strong explanation states this difference explicitly and gives a reason for choosing each, for example using a constant for a fixed rate so that its value is defined once and cannot be accidentally overwritten elsewhere in the program. Explaining the difference in terms of when and how the value may change, rather than only giving definitions, is what examiners look for.
Your focus
- Be able to explain the differences between a variable and a constant.
Which command word is this?
Provide a concise, direct definition without extensive explanation. E.g., 'State the difference between a variable and a constant.'
Constants and variables in a programming language exam tips
Topic Overview
This topic covers the fundamental constructs used to store and manipulate data within computer programs: variables and constants. Both serve as symbolic identifiers mapped to physical memory addresses in RAM, shielding developers from low-level memory management.
Understanding the exact mechanical distinctions between mutable variables and immutable constants is vital for procedural and object-oriented paradigms. It underpins crucial AQA concepts including memory allocation, scope, software robustness, and compiler optimisation.
Key Concepts
- →Identifiers and Memory Mapping: Both variables and constants bind human-readable labels to specific memory addresses and data types.
- →Immutability vs Mutability: Variables allow dynamic reassignment of state during runtime; constants enforce compile-time or runtime write-protection.
- →Elimination of Magic Numbers: Named constants replace raw literals, boosting code clarity and ensuring that future adjustments occur in a single location.
- →Scope and Lifetime: Both constructs possess scope (local vs global) and lifetimes determined by the activation call stack or static storage.
Marking Points
- States that a variable's value can be changed during program execution.
- States that a constant's value is set once and cannot be changed while the program runs.
- Gives a concrete example of an assignment that is valid for a variable but not for a constant.
- Explains a suitable use for a constant, such as a fixed rate or maximum size.
- Explains a suitable use for a variable, such as a running total or a value that changes as input is processed.
Examiner Tips
- 💡Make the contrast explicit by naming the property that differs: whether the value can be changed after it is set.
- 💡Use a short example such as an assignment statement to show the difference clearly.
- 💡Keep your explanation focused on mutability rather than on storage or naming rules, which are shared by both.
- 💡Use the exact phrase 'named memory location' whenever defining either a variable or a constant in definition questions.
- 💡When asked why constants are preferred over literal values, always state two separate aspects: code maintainability (single point of change) and code readability (avoiding 'magic numbers').
Common Mistakes
- Describes a constant as a value that is never used, rather than one whose value cannot be changed.
- Says both variables and constants can be reassigned, so the difference is not identified.
- Gives only a definition of each without explaining how their behaviour differs in use.
- Believing constants take up no memory: Constants still reside in memory (often in data sections or symbol tables) or are inlined directly into machine code instructions by the compiler.
- Confusing a constant with a variable whose value merely happens not to change during a run: A constant is actively protected against modification by the language syntax and translator rules.
- Assuming global variables are preferable for simplicity: Global variables introduce side effects and make debugging difficult; constants are preferred when sharing invariant global configuration data.
Revision Plan
- 1Day 1-2: Master core definitions, focusing on technical terms such as 'named memory address', 'runtime', 'reassignment', and 'immutability'.
- 2Day 3-4: Practice writing standard AQA pseudocode declaring constants and variables with explicit type definitions.
- 3Day 5-6: Review past exam questions concerning software maintainability, global vs local scope, and magic numbers.
- 4Day 7: Complete timed active recall questions on the differences in translator handling between constants and variables.
Exam Question Types
- 📋Short-answer definitions: Stating the formal distinction between variables and constants (usually 2 marks).
- 📋Scenario-based evaluation: Given a pseudocode extract, identify which identifiers should be refactored into constants and justify why (3-4 marks).
- 📋Code tracing with scope: Predicting the output of a program where local and global variables or constants share identifiers (3-5 marks).
Command Word Expectations (AQA)
Provide a concise, direct definition without extensive explanation. E.g., 'State the difference between a variable and a constant.'
Provide reasons and impact. State a characteristic and explain the resulting outcome for the developer or compiler.
Give clear technical arguments supporting why a specific design choice (such as choosing a constant over a variable) is correct for a given context.
How Students Lose Marks (Examiner Pitfalls)
Step-by-Step Worked Solutions
Question: A developer writes an e-commerce checkout module. The standard rate of Value Added Tax (VAT) is 20%. Explain two benefits of declaring VAT as a constant rather than using the literal value 0.20 directly throughout the subroutines. [4 marks]
- 1.Step 1: Identify the role of a constant in code maintenance. If the tax rate alters, updating a single constant declaration propagates changes globally without manual search-and-replace, avoiding omission errors.
- 2.Step 2: Identify the role of a constant in self-documenting code and readability. Using identifier 'VAT_RATE' provides semantic meaning compared to the 'magic number' 0.20, which could be confused with a discount rate.
- 3.Step 3: State the prevention of unintended side-effects. The compiler or interpreter prevents reassignment of a constant, stopping malicious or accidental runtime changes to tax calculations.