Pearson Edexcel · GCSE · Computer Science

    Course version: 1CP2 · Version from Sept 2020

    Understand the difference between and be able to write programs that make…

    Full specification wording

    Subprograms: 6.6.3 understand the difference between and be able to write programs that make appropriate use of global and local variables

    A global variable is declared in the main program and can be read or changed by any subprogram.

    Checked against the Pearson Edexcel 1CP2 specification (opens in a new tab) on .

    • Point 3 of 3
    • 4 key terms

    Study notes

    From the checked teaching notes for this specification point.

    The idea

    A global variable is declared in the main program and can be read or changed by any subprogram. A local variable is created inside a subprogram and exists only while that subprogram runs, so code outside cannot see it. Local variables keep subprograms self-contained: two subprograms can each use a variable named count without clashing. Globals are convenient for values many parts of the program share, such as a score, but overusing them makes code hard to trace because any subprogram might change them. A good approach is to pass needed data in as parameters, keep working values local, and return results, using globals only for genuinely shared state.

    What earns the marks

    • A global variable is declared outside subprograms and is accessible throughout the program.
    • A local variable is declared inside a subprogram and is accessible only within that subprogram.
    • Local variables are created when the subprogram is called and cease to exist when it ends.
    • Two subprograms may each have a local variable with the same name without conflict.
    • Changing a global inside a subprogram affects the value seen elsewhere in the program.
    • Appropriate use means passing data as parameters and returning results where possible, reserving globals for values genuinely shared across the program.

    Common mistakes and how to put them right

    • Assuming a local variable can be read in the main program after the subprogram finishes.

      Put it right: It no longer exists outside its subprogram.

    • Believing a local variable in one subprogram is visible in another.

      Put it right: Each subprogram has its own scope.

    • Thinking a global variable must be used whenever two subprograms need the same data.

      Put it right: Passing parameters and returning values is often clearer.

    • Confusing a parameter with a global.

      Put it right: A parameter is local to the subprogram that receives it.

    Exam tips

    • State the scope of each variable in a code snippet and explain where it can be accessed.
    • When tracing, track global values across calls and treat each local variable as fresh on every call.
    • Justify a design choice by naming the scope that keeps the data available only where it is needed.

    Try the lesson

    The start of the Subprograms: 6.6.3 understand the difference between and be able to write programs that make appropriate use of global and local variables lesson, made from checked teaching notes for this specification point.

    Have a go first

    A program has a subprogram CalculateTax that begins with DECLARE rate : REAL and then rate ← 0.2. No other part of the program uses a variable called rate. Which statement correctly describes rate?

    Listen

    A global variable is declared in the main program and can be read or changed by any subprogram. A local variable is created inside a subprogram and exists only while that subprogram runs, so code outside cannot see it. Local variables keep subprograms self-contained: two subprograms can each use a variable named count without clashing. Globals are convenient for values many parts of the program share, such as a score, but overusing them makes code hard to trace because any subprogram might change them. A good approach is to pass needed data in as parameters, keep working values local, and return results, using globals only for genuinely shared state. A strong answer earns marks for these points. A global variable is declared outside subprograms and is accessible throughout the program. A local variable is declared inside a subprogram and is accessible only within that subprogram. Local variables are created when the subprogram is called and cease to exist when it ends. Two subprograms may each have a local variable with the same name without conflict. Exam tip: State the scope of each variable in a code snippet and explain where it can be accessed.

    Key words

    Say what each word means before you turn the card.

    Scope of variables

    Try answering aloud or in your head before turning the card.

    Global versus local access

    Try answering aloud or in your head before turning the card.

    Lifetime of local variables

    Try answering aloud or in your head before turning the card.

    Appropriate design using parameters and return values

    Try answering aloud or in your head before turning the card.

    See every definition
    Scope of variables
    A global variable can be read or changed by any subprogram, while a local variable is accessible only within the subprogram it is declared in.
    Global versus local access
    A global is declared in the main program and accessible throughout, whereas a local is created inside a subprogram and code outside cannot see it.
    Lifetime of local variables
    Created when the subprogram is called and ceasing to exist when it ends, so it no longer exists outside its subprogram.
    Appropriate design using parameters and return values
    Passing needed data in as parameters, keeping working values local and returning results, using globals only for genuinely shared state.

    There is more to this lesson

    The full lesson goes on to check your understanding and help you earn the marks. Create a free account to carry on, with your progress saved.

    Carry on with the full lesson

    A week of Premium from your first lesson · Then the free plan continues · No card, no charge

    Test your understanding of Subprograms: 6.6.3 understand the difference between and be able to write programs that make appropriate use of global and local variables

    Active recall beats re-reading. Take an instant quiz now to test what you’ve learned and lock it into long-term memory.

    Take topic quiz

    A week of Premium from your first lesson · Then the free plan continues · No card, no charge