StudyDeck

Local & Global Variables

Exam code: 2210
Written by: Ashika|Reviewed by: Caroline Carroll|Updated 2 July 2026

Local Variables

Local Variables

What is a local variable?

  • A local variable is a variable declared within a specific scope, such as a function or a code block

  • Local variables are accessible only within the block in which they are defined, and their lifetime is limited to that particular block

  • Once the execution of the block ends, the local variable is destroyed, and its memory is released

Examples

  • In this code, you can see that the localVariable (with the value 10) is declared inside of the function printValue

  • This means that only this function can access and change the value in the local variable

  • It cannot be accessed by other modules in the program

Psuedocode

PROCEDURE PrintValue()
    DECLARE LocalVariable : INTEGER
    LocalVariable ← 10  // Defines a local variable inside the procedure
    OUTPUT "The value of the local variable is: ", LocalVariable
ENDPROCEDURE

CALL PrintValue()  // Call the procedure

Python

def printValue():
    localVariable = 10  # Defines a local variable inside the function
    print("The value of the local variable is:", localVariable)

printValue()  # Call the function

Global Variables

Global Variables

What is a global variable?

  • A global variable is a variable declared at the outermost level of a program.

  • They are declared outside any modules such as functions or procedures

  • Global variables have a global scope, which means they can be accessed and modified from any part of the program

Examples

  • In this code, you can see that the globalVariable (with the value 10) is declared outside of the function printValue

  • This means that this function and any other modules can access and change the value in the global variable

Psuedocode

DECLARE GlobalVariable : INTEGER
GlobalVariable ← 10  // Defines a global variable

PROCEDURE PrintValue()
    OUTPUT "The value into the variable is: ", GlobalVariable
ENDPROCEDURE

CALL PrintValue()  // Call the procedure

Python

globalVariable = 10 # Defines a global variable

def printValue():
    global globalVariable 
    print("The value into the variable is:", globalVariable)

printValue() # Call the function