Global and local variables
I can explain the difference between a local and global variable.
Global and local variables
I can explain the difference between a local and global variable.
These resources will be removed by end of Summer Term 2025.
Switch to our new teaching resources now - designed by teachers and leading subject experts, and tested in classrooms.
These resources were created for remote use during the pandemic and are not designed for classroom teaching.
Lesson details
Key learning points
- The scope of a variable determines where it can be accessed.
- A local variable is a variable that is only accessible within a specific part of a program.
- A global variable can be accessed anywhere in a program for the duration of the program's execution.
Keywords
Scope - the section of the program where the variable can be accessed and modified
Local variable - a variable that is defined and visible for use only in specific parts of a program, e.g. within a subroutine
Global variable - a variable that is accessible from all parts of a program
Common misconception
Assigning a variable inside a function automatically makes it global.
A variable is local, unless it is specifically set as a global variable.
To help you plan your year 10 computer science lesson on: Global and local variables, download all teaching resources for free and adapt to suit your pupils' needs...
To help you plan your year 10 computer science lesson on: Global and local variables, download all teaching resources for free and adapt to suit your pupils' needs.
The starter quiz will activate and check your pupils' prior knowledge, with versions available both with and without answers in PDF format.
We use learning cycles to break down learning into key concepts or ideas linked to the learning outcome. Each learning cycle features explanations with checks for understanding and practice tasks with feedback. All of this is found in our slide decks, ready for you to download and edit. The practice tasks are also available as printable worksheets and some lessons have additional materials with extra material you might need for teaching the lesson.
The assessment exit quiz will test your pupils' understanding of the key learning points.
Our video is a tool for planning, showing how other teachers might teach the lesson, offering helpful tips, modelled explanations and inspiration for your own delivery in the classroom. Plus, you can set it as homework or revision for pupils and keep their learning on track by sharing an online pupil version of this lesson.
Explore more key stage 4 computer science lessons from the Programming: subroutines unit, dive into the full secondary computer science curriculum, or learn more about lesson planning.
Equipment
Licence
Prior knowledge starter quiz
6 Questions
Q1.A procedure executes a list of commands when called. A function will execute a list of commands and also a value.
Q2.What code would correctly call the function 'calculate'?
123456def calculate(a, b):
total = a + b
print(f"{a} + {b} = {total}")
return total
num1 = 10
num2 = 15Code colourWhen programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
When programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
Q3.What are the parameters in this program?
1234567def calculate(a, b):
total = a + b
print(f"{a} + {b} = {total}")
return total
num1 = 10
num2 = 15
calculate(num1, num2)Code colourWhen programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
When programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
Q4.What Python keyword is used to share a value from a function to the rest of the program?
Q5.A can have one, several or no parameters.
Q6.What is the definition of values held in the brackets of a subroutine call that are passed into a subroutine via the parameters?
Assessment exit quiz
4 Questions
Q1.What value will be output when this program is executed?
123456def example():
number = 10
print(number)
number = 5
example()Code colourWhen programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
When programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
Q2.What will be the output of this program?
12345def multiply(a, b):
answer = a * b
return answer
print(multiply(2, 4))Code colourWhen programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
When programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
Q3.What will be the output of this program?
123456def divide(a, b):
answer = a / b
return answer
divide(8, 2)
print(answer)Code colourWhen programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
When programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
Q4.What will be the output of this program?
123456def add(a, b):
answer = a + b
return answer
solution = add(8, 4)
print(solution)Code colourWhen programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords
When programmers write code, they use a special tool called an IDE (Integrated Development Environment). In an IDE, different colours are used to help programmers understand the code:
- • Blue - numbers and boolean values
- • Green - strings
- • Purple - keywords