Functions

INPUT · Slides

What to use when nothing is passed

01 / 07

Forget to pass and it stops

A function that takes arguments will not run unless you pass them. One short and it errors.

But "it is nearly always the same value, and I still have to write it every time" comes up constantly. If everyone is to be called guest, you would rather not pass it at all.

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

Result

TypeError: greet() missing 1 required positional argument: 'name'

02 / 07

Write a default in

Write = and a value after the argument name and that becomes the default (the value used when nothing is passed).

Pass nothing and you get the default; pass something and that is used instead. One function can now answer to two ways of being called.

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

Result

Hello, guest
Hello, Yui

03 / 07

Arguments with defaults go at the back

An argument with a default has to come after every argument without one. The other way round is a SyntaxError.

The reason is the same as last lesson. Called as order("notebook", 3), Python has no way of settling where the 3 should go. The ones you can leave out go at the back, is how to remember it.

def order(item="pen", count):    print(f"{item} x{count}")

Result

SyntaxError: parameter without a default follows parameter with a default

04 / 07

Pass only some of them

When there are several arguments with defaults, name the ones you want to pass and pass only those. The keyword arguments from last lesson start to earn their keep here.

The third call below skips count and passes only colour. Naming it is what lets you skip over the middle.

def order(item, count=1, colour="black"):    print(f"{item} x{count} in {colour}")order("pen")order("pen", 3)order("pen", colour="red")

Result

pen x1 in black
pen x3 in black
pen x1 in red

05 / 07

This is how the libraries are built

Think back over the tools you have used.

  • sorted(scores) … ascending order without saying how to sort
  • sorted(scores, reverse=True) … say so and you get descending

reverse has a default of False, which is why writing nothing gives you ascending. The separator in split() and the second thing in get(key, 0) are built on the same idea.

Put the common setting in as a default and let the people who want something else write it in. That is why Python's tools are so short to use.

scores = [80, 45, 95]print(sorted(scores))print(sorted(scores, reverse=True))

Result

[45, 80, 95]
[95, 80, 45]

06 / 07

Choosing None as the default

When you want to know inside the function whether something was passed, a default of None is the handy choice.

With 0 or "" as the default you cannot tell "nothing was passed" from "a genuine 0 was passed". This is what the None from Chapter 1 is for.

def show(name, nickname=None):    if nickname is None:        print(name)    else:        print(nickname)show("Yui")show("Yui", "Yu")

Result

Yui
Yu

07 / 07

That is the whole toolkit for functions

The grammar of Chapter 3 is now complete. Make one with def, hand values in with arguments, hand one back with return, pass by name, settle a default.

What is left is how you use it. Where to draw the line round a function, what to call it, how much to make one do. None of that is in a grammar book, and it is what decides whether your code can be read.

The next lesson is the chapter's round-up. No new grammar. Ten questions to build a feel for what goes where.