CHAPTER 3 · 9 lessons · 114 exercises

Functions

Give a chunk of work a name so you can call it any time. This is where your code turns into parts.

0 / 114 solved

Everything you have written so far runs straight down the page. That works, but the moment the same piece of work appears in two places, a fix means fixing both. Three places, three fixes. Writing the same thing over and over is the thickest source of bugs there is.

A function turns that source off. You give a chunk of work a name, and call it by name when you want it. There is one place to change, and the name tells the reader what it does, so the code gets easier to follow.

In Python you make one with def. Write def greet(): and put the body indented underneath. This is where the indenting from Chapter 1 really starts to earn its keep — how deep the whitespace goes is the only thing deciding where the function ends.

You can hand values in (parameters) and get a value back (a return value). def add(a, b): takes two values in, and return a + b sends the answer back to whoever called it. Once "hand in, hand back" clicks, you can start writing programs as parts that fit together.

The most important thing in this chapter is probably the difference between print and return. Both look like "something comes out of the function", and they are nothing alike. print is for a human to look at; return is for the program to use. A printed value cannot go into another calculation, and a returned value never appears on screen unless you ask for it. Carry the two muddled together and you will hit a wall later. We spend a whole lesson on it.

Scope comes up too. A variable made inside a function cannot be seen from outside. That feels like an obstacle at first, but it is looking after you: an i inside a function can never quietly wreck an i outside it.

Later in the chapter come keyword arguments and defaults. You can name a value as you pass it, greet(name="Yui"), and you can decide what to use when nothing is passed, def greet(name="guest"):. Python's libraries are written assuming you know this, so learning it suddenly makes other people's code far easier to read.

Lessons in this chapter

Give your work a name

  1. 45Give it a name and call itdef puts a name on a chunk of work. Defining it does not run it — that is the first hurdle.Go to the exercises
  2. 46Hand a value in as you callWrite a name inside the brackets and you can hand in a different value every time you call.Go to the exercises
  3. 47Ask for the answer backWrite return and the function brings an answer home. You can put it in a variable or use it in a sum.Go to the exercises
  4. 48Show it, or hand it overprint is for a human to look at, return is for the program to use. This is the distinction that matters most in the chapter.Go to the exercises
  5. 49Hand in more than one valueSeparate them with commas and you can catch any number. Just remember that the order carries meaning.Go to the exercises

Inside and outside a function

  1. 50Names that live only inside a functionA variable made inside a function cannot be seen from outside. It looks like an obstacle; it is looking after you.Go to the exercises
  2. 51Naming a value as you pass itPass a value by name, as in greet(name="Yui"), and you stop having to care about the order.Go to the exercises
  3. 52What to use when nothing is passedWrite def greet(name="guest"): and you settle in advance what to use when nothing is handed in.Go to the exercises
  4. 53Deciding what to make a functionThe round-up for Chapter 3. No new grammar, and no three basics — just ten questions about where the lines go.Go to the exercises

Next up is “Facing up to mistakes”. Read an error, track down the cause, and be ready for the unexpected. After this you can get yourself unstuck.