Advanced

Where a name can be seen

A name made inside a function belongs only to that function. Outside, the same name is a different thing, which is what keeps functions from tripping each other.

What you will see

True

About 2 min of reading

Try It

Test what a name inside a function can see.

1

Make a name inside a function and return it.

def make(): total = 10 return total print(make())

Before you run it, say out loud what it will print. Then open the answer.

What it prints
10
2

Try to print that name from outside.

print(total)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
NameError
The name was local. It stopped existing when the function returned.
3

Change a parameter and show the outside value is untouched.

n = 5 def show(x): x = x + 1 return x print(show(n)) print(n)

Before you run it, say out loud what it will print. Then open the answer.

What it prints
6 5
4

Read an outside name inside a function.

limit = 100 def check(v): return v < limit print(check(5))

Before you run it, say out loud what it will print. Then open the answer.

What it prints
True

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

A name assigned inside a function lives only while that function runs, so it cannot be seen or changed from outside.
1 call make
↓
2 total born inside
↓
3 return hands back the value
↓
4 outside, total never existed
Line by line

Read the first example one line at a time. A line that prints shows what appears on screen; the rest set things up quietly.

def make(): sets nothing on screen
total = 10 sets nothing on screen
return total sets nothing on screen
print(make()) 10
Why this works
Local by default

A name assigned inside a function is local. That default is deliberate: it means a function cannot quietly change a name the rest of the program uses.

Why passing beats reaching

A function that reads an outside name depends on something invisible at the call. Passing that value in makes the dependency part of the signature, so the function can be moved or tested alone.

Examples

A name born inside

def make():
    total = 10
    return total

print(make())
print(total)

The first print works. The second fails, because total only exists while make runs. It was never made outside.

Same name, different things

def show(n):
    n = n + 1
    return n

n = 5
print(show(n))
print(n)

The n inside show is its own. Changing it does not change the n outside, which prints 5 again.

Reading an outside name

limit = 100

def check(v):
    return v < limit

print(check(5))

A function can read a name from outside. It is only writing a new one with that name that stays local.

Pass it in instead

def check(v, limit):
    return v < limit

print(check(5, 100))

Passing limit as a parameter makes the dependency visible at the call. The function needs nothing from around it.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← A function that asks for input
Previous Lesson
A function that asks for input
Next Lesson
Returning more than one thing
Returning more than one thing →