Functions – Scope

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Functions – Scope

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What is a local variable?
A A variable created inside a function
B A variable outside all functions
2. What is a global variable?
A A variable inside a function
B A variable created outside all functions
3. def func():\n x = 5\nprint(x)\nWhat happens?
A Prints 5
B NameError x doesn't exist outside func
4. x = 10\ndef show():\n print(x)\nshow()\nWhat prints?
A 10
B Error
5. When does a local variable stop existing?
A When the function finishes
B When the program ends

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1def func(): x = 10 print(x) AOutput: Hi
2count = 0 def add_one(): count += 1 BError: Can't modify global without 'global'
3x = 5 def show(): print(x) show() COk: 'global' lets you modify it
4count = 0 def add(): global count count += 1 DOutput: NameError
5def func(): y = 99 func() print(y) EError: x is local, not visible outside
6msg = 'Hi' def greet(): print(msg) greet() FValid: Read global inside function

Definitions

Scope

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
def func(): x = 5 # x is a _____ variable
2.
x = 10 # x is a ______ variable def func(): print(x)
3.
def func(): y = 5 func() print(y) # causes _________Error
4.
x = 'outside' def func(): x = 'inside' print(x) # prints ________
5.
def add(a, b): result = a + b ______ result

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A def func():
B x = 5
C
D print(x)
Question 2
A def func():
B x = 5
C
D func()
E print(x)
Question 3
A def add(a, b):
B result = a + b
C
D add(3, 5)
E print(result)
Question 4
A name = 'Alice'
B def change():
C name = 'Bob'
D
E change()
F print(name) # expects Bob
Question 5
A def greet():
B msg = 'Hello'
C
D def show():
E print(msg)

Answer Key

Functions – Scope
Section A: Quick Questions
Q1
A — A variable created inside a function
Q2
B — A variable created outside all functions
Q3
B — NameError x doesn't exist outside func
Q4
A — 10
Q5
A — When the function finishes
Section B: Match the Pairs
Q1
def func(): x = 10 print(x) → Error: x is local, not visible outside
Q2
count = 0 def add_one(): count += 1 → Error: Can't modify global without 'global'
Q3
x = 5 def show(): print(x) show() → Valid: Read global inside function
Q4
count = 0 def add(): global count count += 1 → Ok: 'global' lets you modify it
Q5
def func(): y = 99 func() print(y) → Output: NameError
Q6
msg = 'Hi' def greet(): print(msg) greet() → Output: Hi
Section C: Definitions
Q1
Scope — Where a variable can be accessed - local or global.~~Local variable
Section D: Fill in the Blanks
Q1
def func(): x = 5 # x is a local variable
Q2
x = 10 # x is a global variable def func(): print(x)
Q3
def func(): y = 5 func() print(y) # causes NameError
Q4
x = 'outside' def func(): x = 'inside' print(x) # prints 'inside'
Q5
def add(a, b): result = a + b return result
Section E: Spot the Error
Q1
D — x is local to func it can't be accessed outside. Print inside the function.
Q2
E — x is local and dies when func ends. Use return to get the value out.
Q3
E — result is local to add. Return the value and store it outside.
Q4
C — The local name inside change doesn't affect the global name. Use return.
Q5
E — msg is local to greet show can't see it. Pass it as a parameter.