Reference · module 4
Functions
Everything this module explains, on one page.
def
Give a piece of code a name and run it whenever you like.
- def greet(): names a piece of code. The lines under it, indented, are the body — the same colon and the same indentation as an if. Nothing in the body runs when you write it.
- It runs when you call it: greet(). The brackets are the call. Without them, greet is just the name of the function — a value you could store, but not a thing that has happened.
- So a program with a def and no call prints nothing at all, and that is not a bug. Defining is putting a tool on the bench; calling is picking it up.
The mistake you are about to make
def greet(): … and nothing else
def greet(): … then greet()
A file with a def and no call prints nothing, and that is not an error — nobody picked the tool up. The body runs when it is called, and the call is the brackets.
Arguments
Let a function work on something different each time.
- def greet(name): gives the function a parameter. Inside the body, name is an ordinary variable holding whatever the caller handed over.
- The caller supplies the argument: greet("Anna") sets name to "Anna" for that one run. Call it again with something else and the same body does something else.
- Several parameters go in a comma-separated list, and they are matched by position: def area(w, h) called as area(2, 3) gives w = 2 and h = 3. Swap the arguments and you swap the meaning.
The mistake you are about to make
greet() where def greet(name):
greet("Anna")
A parameter in the definition makes the argument compulsory, and empty brackets give a TypeError. Python says exactly how many are missing.
return
Tell returning a value apart from printing one.
- return hands a value back to whoever called the function, so the call itself becomes that value: with def double(x): return x * 2, the expression double(3) is 6 and can be stored or added to.
- print does something else entirely: it puts text on the screen and hands back nothing. A function that prints instead of returning gives you None, and None cannot be added, stored usefully, or passed on.
- return also ends the function immediately. Any lines after it in the same block never run, which is useful on purpose and surprising by accident.
The mistake you are about to make
def double(x): print(x * 2)
def double(x): return x * 2
Print and return both look like ways of producing an answer. Print only shows it; the function itself returns None, and None cannot be added to anything.
Inside and outside
Know which names a function can see, and which it cannot.
- A name created inside a function exists only while that function runs. Reading it afterwards from outside is a NameError — the name was never out there to begin with.
- The other direction is allowed: a function can read a name from outside itself. That is how functions use constants without being handed them every time.
- This is a feature, not a restriction. It means a function cannot quietly break the rest of your program by naming a variable the same thing you did.
The mistake you are about to make
reading a name created inside a function from outside
handing it back with return
A name made inside a function does not exist outside it — it never did, and reaching for it gives a NameError. The way out is return.
Defaults and names
Make an argument optional, and pass one by name.
- def greet(name, greeting="Hi"): gives the second parameter a default. Call greet("Anna") and greeting is "Hi"; call greet("Anna", "Hello") and it is "Hello".
- You can also pass by name: greet(name="Anna", greeting="Hello"). Then the order stops mattering, and the call says what each value is for — worth it as soon as there are three of them.
- Parameters with defaults have to come after those without. Otherwise a call with one argument would be ambiguous, and Python refuses the definition rather than the call.
The mistake you are about to make
def greet(greeting="Hi", name):
def greet(name, greeting="Hi"):
The order looks like a matter of taste. A call with one argument would be ambiguous, so Python rejects the definition itself: defaults go last.