levo

Fundamentals of Programming

Variable scope and stack frames

  • 2 things to know
  • Paper 1

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.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

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

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

A driver is .
softwarehardwarestorage

Blank Blitz makes facts automatic

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

AThe integer 5 is 0101.
B0.75 is 1/2 + 1/4.
CThe fraction bits are 1100.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

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

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

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.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

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