Skip to content
Menu

Year 10 (ages 14–15, Grade 9 in the US) · Computing · GCSE programming · Lesson 10

Local and global variables, and structured programs

Variable scope in Python: local variables inside subroutines and global variables in the main program, why local variables are preferred, and how to structure a program with a main section, comments and meaningful names.

Exam boards: this lesson matches the AQA and Pearson Edexcel GCSE computing courses. Check which course your school follows.

Whiteboard video lesson: Local and global variables, and structured programs

8-minute video lesson

Teaching, worked examples and 20 practice questions solved step by step.

Watch free for a month

What your child will learn

  • Explain local and global scope
  • Predict output involving scope
  • Explain why local variables are preferred
  • Structure a program clearly

The method

  1. Local: created inside a subroutine; only exists there.
  2. Global: created in the main program; visible everywhere.
  3. Prefer parameters and return values over globals.
  4. Structure: comments, names, subroutines, main section.

Key words: scope, local variable, global variable, comment, maintainability.

Worked examples

Worked examples from the lesson, with every step.

Question 1

What happens? c:def make(): c: x = 10 c:make() c:print(x)

  1. Step 1: x is local to make().
  2. Step 2: x doesn't exist outside → NameError

Answer: NameError

Question 2

What is output? c:score = 5 c:def show(): c: print(score) c:show()

  1. Step 1: score is global.
  2. Step 2: A subroutine can read a global.

Answer: 5

Question 3

Rewrite to avoid a global: a subroutine that adds 1 to lives.

  1. Step 1: c:def lose(lives):
  2. Step 2: c: return lives - 1

Answer: pass lives in; return the new value