Functions

INPUT · Slides

Names that live only inside a function

01 / 06

Make it inside, call it outside

Make a variable inside a function, then try to show it outside the function. It looks as though it ought to work, and it does not.

Inside the function good morning came out fine. The moment we stepped outside, Python said it had never heard of the name.

def greet():    word = "good morning"    print(word)greet()print(word)

Result

good morning
NameError: name 'word' is not defined

02 / 06

The range a name reaches is called scope

A variable like word, made inside a function, is a local variable (a variable belonging to that function alone). And the range over which a name works is its scope.

A local variable disappears when the function ends. Call the function again and it starts from nothing. Call the one below twice and you still do not get 2.

def count_up():    number = 0    number += 1    print(number)count_up()count_up()

Result

1
1

03 / 06

A variable outside can be read from inside

The other direction works. A variable made outside a function can be read from inside it.

Lean on that too hard, though, and you end up with a function whose answer depends on what is going on around it. Hand in what it needs as an argument and the next person to read it will thank you.

name = "Yui"def greet():    print(f"Hello, {name}")greet()

Result

Hello, Yui

04 / 06

Assign inside and you get a different variable

This is the part that catches everybody. The moment you write name = ... inside a function, it becomes a new local variable of its own, quite separate from the name outside.

So after the call, the name outside is still Yui. Reading the outer one works, but the moment you try to write to it you get something else entirely — that asymmetry is where people first trip.

name = "Yui"def change():    name = "Haruto"    print(name)change()print(name)

Result

Haruto
Yui

05 / 06

Not an obstacle, a safeguard

"I cannot write to the outside from in here" sounds like a nuisance, but it is there to stop accidents.

The function below uses the name number inside it. If inside and outside were joined up, the important number sitting outside would be wiped out. They are separate, so the outside is untouched.

Whatever names you use inside a function, nothing outside breaks. That is why you can call it safely without knowing what is in it — which is what being a part means.

def total_up(numbers):    total = 0    for number in numbers:        total += number    return totalnumber = "an important note"print(total_up([1, 2, 3]))print(number)

Result

6
an important note

06 / 06

Two windows, and nothing else

The inside and the outside of a function are cut off from each other. On top of that, two windows are provided for values to pass through.

  • arguments … from outside to inside
  • return … from inside to outside

Decide that nothing else gets through and a function becomes a box: put something in, something comes out. Whatever it does inside, the caller need not care.

The next lesson makes the handing in a little more convenient.

def double(number):    return number * 2answer = double(5)print(answer)

Result

10