AQA A-Level Computer Science 7517
Fundamentals of Programming
Fundamentals of Data Structures
Fundamentals of Algorithms
Theory of Computation
Fundamentals of Data Representation
Fundamentals of Computer Systems
Fundamentals of Computer Organisation and Architecture
Fundamentals of Communication and Networking
Consequences of Uses of Computing
Systematic Approach to Problem Solving
Fundamentals of Databases
Big Data
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.
Explain how stack frames account for the two properties of local variables.
Questions from this subtopic
Each one with the model answer underneath.
- 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
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
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
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
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
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.
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.