Functions

INPUT · Slides

Give it a name and call it

01 / 07

Writing the same thing over and over

To greet somebody three times, all you can do at the moment is write print three times.

Three is bearable. But once the same work is scattered across ten places and you decide good morning should say something else, that is ten places to fix — and one miss is a bug.

print("good morning")print("good morning")print("good morning")

Result

good morning
good morning
good morning

02 / 07

def puts a name on it

A chunk of work with a name on it is called a function. You make one by writing def.

  • after def comes the name of the function
  • after the name comes ()
  • the line ends with :
  • the work goes underneath, indented by four spaces

It is exactly the colon-and-indent shape you already know from if and for. To use it, you write the name with () after it — that is what calling means.

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

Result

good morning

03 / 07

Defining it does not run it

Here is the first hurdle. Writing def does not run what is inside.

The def line asks Python to remember that a piece of work goes by that name. It does not ask for it to be done. Nothing below says good morning.

Remembering is the def line; doing is the greet() line. Those are two entirely separate jobs.

def greet():    print("good morning")print("the end")

Result

the end

04 / 07

Make it once, call it as often as you like

This is where a function starts to pay. Make it once and you can call it any number of times.

And when you want to change it, there is one place inside the function to change. A hundred calls, still one edit.

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

Result

good morning
good morning
good morning

05 / 07

Where the inside of a function ends

The only thing deciding how far the function goes is the depth of the indent, just as it was with if.

A line brought back to the left is already outside. In the example below, this is outside comes out first — the def block was only being remembered, and nothing runs until it is called.

def greet():    print("good morning")    print("how are you")print("this is outside")greet()

Result

this is outside
good morning
how are you

06 / 07

Make it before you call it

The order matters too. Call it above the def and you get a NameError.

Python reads from the top down, so until it reaches the def line it has never heard of that name. Make the function first, then call it.

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

Result

NameError: name 'greet' is not defined

07 / 07

A name is what makes code readable

Put work in a function and you can read the code by what it is called.

You do not need to know what is inside divider to see that it draws a line. The intention comes across without reading the body — that is the real reward for naming things.

That said, this function says the same thing every time. Next we let you hand a value in as you call it.

def divider():    print("--------")divider()print("total 520")divider()

Result

--------
total 520
--------