New
New
Year 10
AQA

Global and local variables

I can explain the difference between a local and global variable.

New
New
Year 10
AQA

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

  1. The scope of a variable determines where it can be accessed.
  2. A local variable is a variable that is only accessible within a specific part of a program.
  3. 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...

Unplugged activities are a great way of visualising key programming concepts. This topics lends itself well to sharing information widely compared to small groups and an activity in the classroom exploring this would help pupils understand the scope of variables.
Teacher tip

Equipment

Licence

This content is © Oak National Academy Limited (2025), licensed on Open Government Licence version 3.0 except where otherwise stated. See Oak's terms & conditions (Collection 2).

Lesson video

Loading...

Prior knowledge starter quiz

Download quiz pdf

6 Questions

Q1.
A procedure executes a list of commands when called. A function will execute a list of commands and also a value.
Correct Answer: return
Q2.
What code would correctly call the function 'calculate'?
123456
def calculate(a, b): total = a + b print(f"{a} + {b} = {total}") return total num1 = 10 num2 = 15
Code colour

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
Correct answer: `calculate(num1, num2)`
call(calculate)
def (num1, num2)
Q3.
What are the parameters in this program?
1234567
def calculate(a, b): total = a + b print(f"{a} + {b} = {total}") return total num1 = 10 num2 = 15 calculate(num1, num2)
Code colour

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
num1, num2
total
calculate
Correct answer: a, b
Q4.
What Python keyword is used to share a value from a function to the rest of the program?
Correct Answer: return
Q5.
A can have one, several or no parameters.
Correct Answer: function, procedure
Q6.
What is the definition of values held in the brackets of a subroutine call that are passed into a subroutine via the parameters?
procedures
parameters
Correct answer: arguments
functions

Assessment exit quiz

Download quiz pdf

4 Questions

Q1.
What value will be output when this program is executed?
123456
def example(): number = 10 print(number) number = 5 example()
Code colour

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
Correct answer: 10
5
number
15
Q2.
What will be the output of this program?
12345
def multiply(a, b): answer = a * b return answer print(multiply(2, 4))
Code colour

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
multiply 2, 4
Correct answer: 8
6
nothing, because the return value is not held in a variable
Q3.
What will be the output of this program?
123456
def divide(a, b): answer = a / b return answer divide(8, 2) print(answer)
Code colour

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
divide 8, 2
8 divided by 2 is 4
4
Correct answer: an error message
Q4.
What will be the output of this program?
123456
def add(a, b): answer = a + b return answer solution = add(8, 4) print(solution)
Code colour

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
Correct answer: 12
add 8, 4
solution
an error message