Local & Global Variables (Cambridge (CIE) O Level Computer Science)

Revision Note

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

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

Python example

  • In this python 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

Local variables

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

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

Python example

  • In this python 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

Global variables

globalVariable = 10 # Defines a global variable

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

printValue() # Call the function

Last updated:

You've read 0 of your 5 free revision notes this week

Sign up now. It’s free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Author: James Woodhouse

Expertise: Computer Science

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.