B2.1.1 · Programming fundamentals
Variables and scope
By the end you can
Declare, initialize, and trace global and local variables across stack frame scopes, distinguishing scope, lifetime, and shadowing.
A is a named location in memory used to hold data that may change during program execution. When declaring a variable, a programming language allocates memory and determines which operations are valid for that data type.
Data types and memory#
In typed languages like Java, every variable must have a declared data type so the compiler knows how many bytes to allocate. In dynamically typed languages like Python, types are inferred at runtime, but the underlying data structures still adhere to strict type rules:
- Integer (int): Whole numbers without fractional parts (e.g.
-5,0,42). Usually 4 bytes (32-bit two's complement). - Float / Real (float / double): Numbers with decimal points (e.g.
3.14159,-0.05) represented via IEEE-754 floating-point standards. - Boolean (bool / boolean): Truth values taking either
trueorfalse. Used to control conditional branching and loops. - Character (char): A single ASCII or Unicode character (e.g.
'A','#'). Python treats single characters as strings of length 1. - String (str / String): An alphanumeric sequence of characters. In object-oriented languages, strings are reference types equipped with built-in methods.
Scope, lifetime and stack frames#
The of an identifier refers to the region of program source code where the variable is recognised and accessible. The refers to the period of time during execution when memory remains allocated to that variable.
Global variables are declared outside all functions. They are allocated when the program starts and remain until termination. While convenient, excessive reliance on global variables makes programs difficult to maintain and test, because any subroutine can silently alter global state.
Local variables are declared within a block or subroutine. When a function is called, the CPU pushes a new onto the call stack containing parameters and local variables. When the function returns, its stack frame is popped, and all local variables are destroyed immediately.
1. Program Starts (Global Scope Initialised)
The global execution context is created in memory. Global variables "taxRate" and "currency" are allocated and accessible by any function.
Active Call Stack & Scope Frames (Top of Stack = Current Scope)
Check yourself
Variables and scope
Quick questions, made fresh every time and marked here. Do a few until they feel easy.
Exam practice
Paper 1-style questions
3 questions · 8 marks
Write your answer as you would in the exam, then show the mark points and tick each one your answer made. These are our own questions in the style of the exam, not past-paper questions.
1.Distinguish between a local variable and a global variable in terms of scope, lifetime, memory allocation and software design risks.
[4]Distinguish: Make the differences between two things clear.
2.A programmer declares a global variable 'rate = 0.05' and inside a function declares 'rate = 0.10'. Explain what happens when 'rate' is referenced inside that function.
[2]Explain: Give reasons: say what happens AND why, or how one thing causes another.
3.Distinguish between primitive data types and reference or object data types in terms of value storage and memory.
[2]Distinguish: Make the differences between two things clear.
In a nutshell
- A variable is a named space in memory used to store data that may change during program execution.
- Data types dictate the memory representation and permitted operations: integers, floating-point decimals, booleans, characters and strings.
- Global variables are accessible throughout the program and exist for its entire lifetime.
- Local variables exist inside a subroutine activation record on the call stack and are deallocated immediately upon function return.
- Variable shadowing occurs when a local variable shares the same name as a global variable, overriding it inside that local scope.
