Choose exam board for KS4 Computer Science (GCSE)
Choose exam board for KS4 English
Choose exam board for KS4 French
Choose exam board for KS4 Geography
Choose exam board for KS4 German
Choose exam board for KS4 History
Choose tier for KS4 Maths
Choose exam board for KS4 Music
Choose exam board for KS4 Physical education (GCSE)
Choose exam board for KS4 Religious education (GCSE)
Choose exam board for KS4 Spanish

      Global and local variables in programs

      Lesson details

      Learning outcome

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

      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 subprogram

      • 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.

      Teacher tip

      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.

      Equipment

      Pupils will need access to a device that can edit and run Python programs. Examples in this lesson use the RPF Code Editor https://oak.link/python-editor

      Licence

      This content is © Oak National Academy Limited (2026), 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

      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

      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

      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 subprogram call that are passed into a subprogram via the parameters?

      procedures
      parameters
      Correct answer: arguments
      functions

      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

      Correct answer: 10
      5
      number
      15

      Q2.
      What will be the output of this program?
      123456
      def multiply(a, b): answer = a * b return answer print(multiply(2, 4))
      Code colour

      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?
      1234567
      def divide(a, b): answer = a / b return answer divide(8, 2) print(answer)
      Code colour

      divide 8, 2
      8 divided by 2 is 4
      4
      Correct answer: an error message

      Q4.
      What will be the output of this program?
      1234567
      def add(a, b): answer = a + b return answer solution = add(8, 4) print(solution)
      Code colour

      Correct answer: 12
      add 8, 4
      solution
      an error message

      To help you plan your 10 computer science lesson on: Global and local variables in programs, download all teaching resources for free and adapt to suit your pupils' needs...