Skip to topic
    ← Back to course topics

    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

    1. 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
    1. 1Day 1-2: Master core definitions, focusing on technical terms such as 'named memory address', 'runtime', 'reassignment', and 'immutability'.
    2. 2Day 3-4: Practice writing standard AQA pseudocode declaring constants and variables with explicit type definitions.
    3. 3Day 5-6: Review past exam questions concerning software maintainability, global vs local scope, and magic numbers.
    4. 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)
    State

    Provide a concise, direct definition without extensive explanation. E.g., 'State the difference between a variable and a constant.'

    Explain

    Provide reasons and impact. State a characteristic and explain the resulting outcome for the developer or compiler.

    Justify

    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)
    Pitfall: Stating that a constant is a value that 'never changes' without specifying that it cannot be altered during program execution (at runtime).
    ❌ Weak Answer (Loses Marks):A constant is a number that stays the same, while a variable can change.
    Example improved answer:A variable is a named memory location whose stored value can be altered during program execution (runtime). A constant is an identifier associated with a memory location whose value is bound at declaration and cannot be reassigned or modified while the program executes.
    Examiner Tip: Always refer to named memory locations and program execution or runtime to secure technical marks.
    Pitfall: Failing to explain the software engineering advantages of constants beyond simply 'saving memory' (which is technically incorrect).
    ❌ Weak Answer (Loses Marks):Constants make the program run faster and take up less RAM than variables.
    Example improved answer:Using named constants prevents accidental runtime modification through compile-time type-checking or syntax errors. It also improves maintainability; updating a value requires changing it at a single declaration point rather than searching for hardcoded literals throughout the codebase.
    Examiner Tip: Focus on maintainability, readability, and avoiding accidental side effects when evaluating constant usage in Paper 1 or Paper 2.
    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. 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. 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. 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.
    Final Answer: 1. Maintainability: If VAT changes, it only needs altering in one location at declaration, eliminating errors from missed updates. 2. Self-documenting code / Reliability: Replacing magic numbers with an identifier improves readability and prevents accidental runtime overwriting by triggering a compilation error if re-assigned.
    Active Recall Memory Test
    What is the precise definition of a variable in Computer Science?
    Key Fact: A named memory location whose stored value can change during program execution (at runtime).
    What is a 'magic number' in source code and why should it be avoided?
    Key Fact: A hardcoded literal numeric value without explanation; it reduces readability and makes code updates prone to errors.
    What occurs when code attempts to reassign a value to an identifier declared as a constant?
    Key Fact: The compiler or interpreter flags a syntax or translation error, halting compilation or throwing an exception.
    Frequently Asked Questions
    Why use constants if variables can just be left unchanged?
    While a programmer might intend never to change a variable, human error or complex logic can accidentally reassign it. Declaring it as a constant enforces write-protection via the compiler or interpreter. It also signals programmer intent to anyone else reading the code.
    Do constants use less memory than variables in RAM?
    Not necessarily. In high-level languages, constants take up equivalent storage determined by their data type. However, compilers can sometimes optimise constants by replacing them with immediate values directly inside machine code instructions, avoiding memory lookup operations.
    Can a constant be defined using a calculation?
    Yes, provided the expression can be evaluated at compile time before execution starts. For instance, declaring 'SECONDS_IN_DAY = 60 * 60 * 24' is common practice because the result is deterministic and fixed.
    What naming conventions are expected for constants in AQA exams?
    AQA pseudocode typically writes constant names in uppercase letters with underscores between words (e.g., 'MAX_ARRAY_SIZE'). While not strictly penalised if lowercased, using uppercase matches exam mark scheme conventions.