levo

Fundamentals of Programming

Variable scope and stack frames

  • 2 things to know
  • Paper 1
Start revising · £5/month

Overview

Recall questions on local and global variables, why locals are preferred, what happens when a local shadows a global, and how stack frames explain a local's lifetime.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

What happens when a local variable has the same name as a global one?

Key term

Explain how stack frames account for the two properties of local variables.

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What is a local variable, and what two properties does it have?

    Model answer

    A variable declared inside a subroutine. It exists only while that subroutine is executing, being created when the subroutine is called and destroyed when it returns, and it is accessible only within that subroutine, so no code outside can read or change it.

  2. 2

    Explain why it is good practice to use local variables.

    Model answer

    Because the subroutine becomes self-contained: it cannot accidentally change data belonging to the rest of the program, and the rest of the program cannot interfere with it, which makes it safe to test on its own and to reuse in another program. The same name may be used freely in different subroutines without a clash, and the memory is released when the subroutine returns. It also narrows the search when debugging, since only the code of that one subroutine can have altered the variable.

  3. 3

    What is a global variable?

    Model answer

    A variable declared outside all subroutines, in the main program. It is accessible from anywhere in the program, including inside every subroutine, and it exists for as long as the program runs.

  4. 4

    How does a local variable differ from a global one?

    Model answer

    A local variable is declared inside a subroutine, a global one outside all of them. A local can be used only within the subroutine that declares it; a global can be used anywhere. A local exists only while its subroutine runs and its value is lost on return; a global exists and holds its value for the whole run. Locals occupy memory only while needed, globals throughout. The same local name may be reused in several subroutines without conflict, whereas a global name must be unique across the program.

  5. 5

    Give the disadvantages of using global variables.

    Model answer

    Any part of the program can change them, so tracing which statement caused a wrong value is difficult and errors can appear far from their cause. A subroutine that depends on a global is no longer self-contained, so it cannot be tested alone or reused in another program without carrying the global with it. Global names must all be distinct, which is awkward in a large program written by several people, and the memory stays allocated for the whole run.

  6. 6

    When is using a global variable justified?

    Model answer

    When a value genuinely has to be available throughout the program and passing it as a parameter to every subroutine would be unwieldy, for example a fixed configuration setting, a constant such as a tax rate, or a data structure that the whole program is built around. Even then it is safer to make it a named constant if it never changes.

Other ways to work this

The same content, four other ways.

Everything in your subscription

Runs, plus a mode for every other kind of session.

1Flash
2Imposter
3Fade
4Brain dump

Learn every subtopic in runs of four questions

Facts flash and fade, a wrong word hides in some of them, and a brain dump decides which questions are locked in.

Fundamentals of Programming
Fundamentals of Data Structures
Fundamentals of Algorithms
Theory of Computation

Cram the night before, weakest topic first

A quick scan finds your weakest topics. Recall each fact you missed three times in a row.

A driver is .
softwarehardwarestorage

Blank Blitz consolidates the facts

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

✓What it iscovered
~Why it happenspartly there
How long it lastsnot there yet

Explain It tests your understanding

Explain a concept in your own words and see which of its points you covered.

Head means the first single element of a non-empty list.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The faults are the mistakes students make most often.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Fundamentals of Programming, on a schedule.

Learn it in runs of four questions, each ending on a brain dump against the clock. Every question you lock in comes back the next day, then at longer gaps, each time just before you would forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.