Scope Examples: 45 Problems with Answers

Worked examples of Scope to try before you open each solution, then practice problems with answers.

Worked Examples

Example 1

medium
What is the output? SET x = 10. FUNCTION addFive(): SET x = 5. RETURN x + 5. OUTPUT addFive(). OUTPUT x.
Show solution

Answer

10, then 10. The function's local x does not affect the global x.

First step

1.
Step 1: The function creates a local variable x = 5 (separate from the global x = 10).

See the full worked solution + why-it-works coaching

SetupKey insightWhy it worksCommon pitfallConnection

Free: full solutions on 3 topics a day. No card.

Every topic, every day: the Family plan

Example 2

hard
Explain why using global variables inside functions is generally discouraged.

Example 3

medium
What single keyword added to `def add(n): total = total + n` (with global `total = 0`) lets it modify the global?

Example 4

hard
`counter = 0`. `def inc(): global counter; counter += 1`. After calling `inc()` 5 times, what is `counter`?

Example 5

hard
Explain why `def f(): x = x + 1` raises an error even though there is a global `x = 5`.

Practice Problems

Try each one, then open the solution to compare your method.

Example 1

medium
What is the output? SET y = 100. FUNCTION test(): OUTPUT y. test(). Will this work, or will there be an error?

Example 2

medium
What is the output? Explain any errors.

SET count = 0

FUNCTION increment()
SET count = count + 1
SET bonus = 10
END FUNCTION

increment()
OUTPUT count
OUTPUT bonus

Example 3

easy
`x = 5` is defined outside any function. Inside `def f(): print(x)`, can f read `x`?

Example 4

easy
`def f(): y = 3`. After calling `f()`, can the outside code read `y`?

Example 5

easy
Two functions each define their own `count`. Do they interfere?

Example 6

easy
`def f(): return z`. There is no `z` anywhere. What happens when `f()` runs?

Example 7

easy
A parameter `def f(n): ...`. Is `n` local or global to `f`?

Example 8

easy
`g = 10`. `def f(): g = 20`. Without `global`, does `f()` change the outer `g`?

Example 9

easy
Inside `def f(): print(PI)` with global `PI = 3`, what prints?

Example 10

easy
Does a local variable created in `f` still exist after `f` returns?

Example 11

medium
`x = 1`. `def f(): x = 2; print(x)`. After `f()` and then `print(x)` outside, what prints (in order)?

Example 12

medium
`count = 0`. `def inc(): global count; count = count + 1`. After two `inc()` calls, what is `count`?

Example 13

medium
`def f(): n = n + 1` with global `n = 5`, no `global` declaration. What happens on `f()`?

Example 14

medium
Shadowing: global `temp = 100`. `def f(): temp = 1; return temp`. What does `f()` return?

Example 15

medium
Pass-by-value feel: `def f(a): a = a + 1`. With `x = 5`, after `f(x)` what is `x` outside?

Example 16

medium
Nested read: `g = 7`. `def outer(): print(g)`. Is `g` resolved as local or global?

Example 17

medium
Why prefer parameters over globals? `def area(w, h): return w * h` vs reading global `w`,`h`.

Example 18

medium
`s = "hi"`. `def f(): s = "bye"; return len(s)`. What does `f()` return?

Example 19

medium
`def f(): a = 1; def g(): return a + 1; return g()`. What does `f()` return?

Example 20

challenge
Trace LEGB: `x = "global"`. `def outer(): x = "enclosing"; def inner(): return x; return inner()`. What does `outer()` return?

Example 21

challenge
`total = 0`. `def add(n): total = total + n`. Why does `add(5)` raise an error, and what is the one-keyword fix?

Example 22

challenge
Counter via default vs global: which keeps state between calls cleanly? `def f(): global c; c+=1` (with `c=0`) vs recreating a local each call.

Example 23

easy
`x = 10`. `def f(): print(x)`. What does calling `f()` print?

Example 24

easy
`def f(): a = 5`. After calling `f()`, what is the value of `a` outside the function?

Example 25

easy
Two functions each define `x = 1` inside their own bodies. Do they share that x?

Example 26

easy
`def f(n): return n + 1`. Is `n` local or global?

Example 27

easy
Global `MAX = 100`. `def f(): print(MAX)`. What does `f()` print?

Example 28

medium
`x = 5`. `def f(): x = 10; print(x)`. After calling `f()`, what does `print(x)` (outside) display?

Example 29

medium
`total = 0`. `def add(n): total = total + n`. What happens on `add(5)`?

Example 30

medium
`def outer(): x = 1; def inner(): print(x); inner()`. What does `outer()` print?

Example 31

medium
`def f(a): a = a * 2`. With `x = 7`, after `f(x)`, what is `x`?

Example 32

medium
`xs = [1,2,3]`. `def f(lst): lst.append(4)`. After `f(xs)`, what is `xs`?

Example 33

medium
`def f(): x = 1; def g(): x = 2; g(); return x`. What does `f()` return?

Example 34

medium
`def f(): x = 1; def g(): nonlocal x; x = 2; g(); return x`. What does `f()` return?

Example 35

medium
In a function `def f(): print(len([1,2,3]))`, what scope is `len` found in?

Example 36

medium
`x = 1`. `def f(): print(x); x = 99`. What happens when `f()` is called?

Example 37

hard
`x = 'A'`. `def outer(): x = 'B'; def inner(): x = 'C'; return x; return inner() + x`. What does `outer()` return?

Example 38

hard
`def make_counter(): count = 0; def step(): nonlocal count; count += 1; return count; return step`. Calling `c = make_counter(); print(c(), c(), c())` prints what?

Example 39

hard
`y = 10`. `def f(y=y): return y`. The global y is changed to 99 before calling `f()`. What does `f()` return?

Example 40

challenge
`x = 'G'`. `def outer(): x = 'O'; def middle(): def inner(): return x; return inner(); return middle()`. What does `outer()` return?

What to focus on

Core idea: Scope prevents name collisions. Two functions can each have a variable called 'count' without interfering with each other.

Common stuck point: Modifying a global variable inside a function usually requires a special keyword (global in Python). Without it, you create a new local variable.

Ask yourself: Does the answer depend on whether a name created inside a function can be seen or changed from somewhere else?

Concept recap

The region of a program where a variable is accessible. Variables defined inside a function have local scope and exist only within that function. Variables defined outside all functions have global scope and are accessible from anywhere in the program.

Each function is a room with one-way glass. From inside, you can look out and read the notices on the hallway board, the globals; from the hallway, nobody can see into the room. Writing a name inside the room pins a new note on the room's own wall rather than changing the hallway board, unless you declare global.

Read the full lesson on Scope

Background knowledge

These ideas may be useful before you work through the harder examples.

function programmingreturn values