Basics · lesson 8 of 10
Parts in this lesson
Adef name():makes a function
Bname(a, b)calls it, with values
Creturngives back a result
Ddays=14a default value
ENonePython's "nothing"

Functions

Last updated

As programs grow, the same few lines turn up in several places. In this lesson you'll give a group of lines a name with def, pass values in, get an answer back with return, give inputs default values, and see what a function gives back when it forgets to return.

By the end of this lesson you can

  • Define a function with def and call it
  • Pass values in through parameters, by position or by name
  • Give back a result with return, and give parameters default values
  • Explain why a function with no return gives None

Your own function

def, short for define, makes a function: a group of lines with a name. Like if, the line ends with a colon and the function's lines are indented under it. Making a function doesn't run those lines. They run each time something calls the function, by writing its name and brackets: show_rules().

Watch it run. Python reads the def on line 1, skips over the function's lines, and only runs them when line 6 calls it, and again when line 8 does.

Step 1 code main.py
Edit the code, then run it again

You've been calling functions since lesson 1.1: print, len, input and int are all functions that come with Python. Now you can make your own. The two empty lines after a function aren't needed, but PEP 8, Python's style guide, asks for them so functions stand out.

Values in: parameters

A function becomes far more useful when it can work with different values. The names in the brackets of the def line, name and books, are parameters. When you call the function, the values in the brackets, called arguments, are matched to the parameters by position. "Sam" goes to name, 2 goes to books.

Watch it run. While welcome runs, the table shows only its names: name and books exist only inside the function.

Step 2 code main.py
Edit the code, then run it again

You can also match arguments by name: welcome(books=2, name="Sam") does the same thing, in any order. These are the keyword arguments you met with sep= and end= in lesson 1.2.

A result out: return

return ends the function and gives a value back to the line that called it. The call fine_for(3) is then replaced by that value, so first = fine_for(3) gives first the value 75.

Step 3 code main.py
Edit the code, then run it again

Printing shows a value to the person using the program. Returning hands it back to the program, so it can be stored, added up or passed to another function. Most functions should return their answer and let the code that called them decide what to do with it.

Default values

A parameter can have a default value, written with = on the def line. If a call leaves that argument out, the default is used. Loans usually last 14 days, so due_in() with nothing in the brackets means 14.

Step 4 code main.py
Edit the code, then run it again

Parameters with defaults must come after the ones without. def due_in(days=14, title): is a SyntaxError, because Python couldn't tell which value was meant for which name.

Printing instead of returning

This fine_for prints the fine instead of returning it. Press Run and read the output, then the error from the bottom up.

Step 5 code main.py
Edit the code, then run it again

The fines 75 and 125 were printed, but then line 5 stopped with unsupported operand type(s) for +: 'NoneType' and 'NoneType'. A function that ends without return gives back None, Python's value for "nothing". So line 5 tried to work out None + None.

Fix it by changing print to return on line 2. Then the calls give back numbers, and line 6 prints 200.

Check what you learned

Four questions. Pick or type an answer, then check it.

What does this program print?
def double(n):
    return n * 2


print(double(double(3)))
def welcome(name, books):. Which call gives books the value 3?
What does a function give back if it has no return?
def due_in(days=14):. What does due_in() use for days?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Python