Hero background

Understanding Variable Scope

Technology • Year 10 • 75 • 24 students • Created with AI following Aligned with National Curriculum for England

Technology
0Year 10
75
24 students
20 March 2025

Teaching Instructions

create a lesson plan for year 10 compute science exam board OCR - on subroutines - scope Learning objectives

Describe scope of variables Describe how parameters can reduce the need for global variables Identify when to use global variables Describe a constant

Understanding Variable Scope

Lesson Details

  • Subject: Computer Science
  • Exam Board: OCR (J277 Computer Science)
  • Year Group: Year 10
  • Lesson Duration: 75 minutes
  • Class Size: 24 students

Curriculum Coverage

This lesson aligns with the OCR GCSE (9-1) Computer Science specification, specifically:

  • 2.2.1 The use of variables, constants, operators, inputs, outputs, and assignments.
  • 2.2.2 The use of the three basic programming constructs used to control the flow of a program: sequence, selection, iteration.
  • 2.2.3 The use of basic string manipulation.
  • 2.2.4 The use of subroutines (procedures and functions) with parameters and return values.

Lesson Objectives

By the end of this lesson, students will be able to:

  1. Describe the scope of variables (local vs global).
  2. Explain how parameters can reduce the need for global variables in subroutines.
  3. Identify when it is appropriate to use global variables.
  4. Describe what a constant is and how it differs from a variable.

Lesson Structure

Starter Activity (10 minutes) – Interactive Class Discussion

  • Engagement Question: “If we create a variable inside a function, can we use it outside the function? Why or why not?”
  • Write three statements on the board:
    • “A local variable can be accessed anywhere in a program”
    • “Global variables can be useful but can also cause problems”
    • “Constants cannot change once assigned.”
  • Ask students to discuss in pairs and decide if these statements are true or false.
  • Whole-class discussion to consolidate initial ideas.

Main Activities (50 minutes)

Activity 1: Explaining Variable Scope (15 minutes)

  • Teacher explanation (5 minutes) using a whiteboard or smartboard:
    • Define a local variable (a variable that exists only inside a subroutine).
    • Define a global variable (a variable declared outside any subroutine and accessible throughout the program).
    • Real-world analogy:
      • Local Variables are like a temporary passcode for a class test – only applicable inside that classroom.
      • Global Variables are like a school-wide policy – the same for everyone, everywhere in the school.
  • Code Example (5 minutes) in Python:
    def greet():
        message = "Hello!"  # Local variable
        print(message)
    
    greet()
    print(message)  # This will cause an error
    
    • Question: Why does the second print statement cause an error?
  • Class Discussion (5 minutes): Students discuss and explain why the error occurs.

Activity 2: Parameters vs. Global Variables (15 minutes)

  • Teacher explanation (5 minutes):

    • How parameters allow data to be passed into subroutines instead of using global variables.
    • Why using global variables too often leads to coding issues (e.g., unintended changes, difficulty debugging).
  • Python demonstration (5 minutes) – Contrast poor vs. good practice:

    # Using a global variable (Not recommended)
    name = "Alice"
    
    def greet():
        print("Hello, " + name)
    
    greet()
    
    # Using a parameter instead (Better practice)
    def greet(name):
        print("Hello, " + name)
    
    greet("Alice")
    
  • Think-Pair-Share (5 minutes) – students rewrite an example with unnecessary global variables to use parameters instead.


Activity 3: When to Use Global Variables (10 minutes)

  • Guided Discussion (4 minutes)
    • When can global variables make sense in a program? (e.g., Keeping a high score in a game? Storing configuration settings?)
  • Small Group Challenge (6 minutes)
    • Each group of four students gets a scenario (e.g., creating a scoreboard, storing user settings, etc.).
    • They decide whether global or local variables would be best and explain why.
    • Groups share their answers briefly.

Activity 4: Constants vs. Variables (10 minutes)

  • Teacher explanation (5 min):
    • A variable’s value can change during program execution.
    • A constant does not change once assigned (e.g., tax rate, speed of light).
  • Python example (5 min):
    PI = 3.14159  # Constant (usually written in uppercase)
    
    def circle_area(radius):
        return PI * radius * radius  # Cannot change PI
    
    print(circle_area(5))
    
    • Question: Why is PI written in uppercase? What happens if we try to change it?

Plenary (15 minutes) – Exit Ticket & Reflection

  • Quick Quiz (7 minutes) – Multiple choice questions on variable scope, parameters, and constants.
  • Mini Reflection (8 minutes) – Each student writes one thing they learned today and one question they still have.

Assessment & Differentiation

  • Low ability:
    • Provide scaffolded worksheets with code snippets and gap-fill explanations.
    • Use visual diagrams to illustrate variable scope.
  • Mid ability:
    • Encourage students to debug incorrect code with variable scope issues.
  • High ability:
    • Challenge students to write a program using only local variables and parameters—avoiding global variables entirely.

Homework/Extension

  • Research and prepare an explanation, with examples, on why excessive use of global variables is considered bad practice in large-scale software development.
  • Challenge: Write a Python program that asks for a user’s name and prints a greeting without using any global variables.

Resources Required

  • Interactive Python environment (e.g., Replit or IDLE)
  • Printed worksheets for scaffolding
  • Whiteboard for live explanations

This lesson follows UK education standards and is structured to engage students using multiple teaching methods—including discussion, coding, collaboration, and hands-on coding challenges. 🚀

Create Your Own AI Lesson Plan

Join thousands of teachers using Kuraplan AI to create personalized lesson plans that align with Aligned with National Curriculum for England in minutes, not hours.

AI-powered lesson creation
Curriculum-aligned content
Ready in minutes

Created with Kuraplan AI

🌟 Trusted by 1000+ Schools

Join educators across United Kingdom