Global and Local Variables (OCR A-Level Computer Science): Flashcards
Practise the cards
15 cards from this deck
ShowHide
Practise the cards
15 cards from this deck
Variables (definition)
Variables (definition)
Named storage locations in memory used to hold data
Scope (definition)
Scope (definition)
Where a variable can be accessed within a program
Global variables (definition)
Global variables (definition)
Variables declared outside any function, class, or block
Global variables: scope
Global variables: scope
Accessible from any part of the program
Global variables: lifetime
Global variables: lifetime
Exist for the entire duration of the program
Local variables (definition)
Local variables (definition)
Variables declared within a function, class, or block
Local variables: scope
Local variables: scope
Accessible only within the block/function where defined
Local variables: lifetime
Local variables: lifetime
Exists only while the function or block is executing
Global vs local: memory usage
Global vs local: memory usage
Global occupy memory throughout; local memory freed after use
Global variables: benefit (data sharing)
Global variables: benefit (data sharing)
Useful for data accessed & modified by multiple functions
Global variables: drawback (errors)
Global variables: drawback (errors)
Accidental modification can lead to unintended behaviour
Local variables: benefit (memory)
Local variables: benefit (memory)
Memory allocated & freed dynamically, reducing consumption
Local variables: drawback (access)
Local variables: drawback (access)
Cannot be accessed outside their defining function or block
Shadowing (definition)
Shadowing (definition)
Local variable with same name as a global variable
Converting global to local: method
Converting global to local: method
Pass it as a parameter to functions that need it
