Your first function
Picture a recipe card. A recipe is written down once, and you cook from it as often as you like: for two people today, for ten tomorrow. The card says what to put in, what to do with it and what dish comes out at the end.
In a program, such a card is a function of your own. When the same calculation is needed in several places, it is written once and given a name. The function add takes two numbers and returns their sum:
def add(a, b):
return a + b
print(add(2, 3)) # 5
print(add(10, -4)) # 6Here the recipe takes the first two lines and is cooked twice: with 2 and 3, then with 10 and -4.
Definition and call
Let's take the function add apart piece by piece.
The def line
The line def add(a, b): starts a function definition. It is built like this:
- The word
defsays that a new function follows. addis its name, which is used to call it.- The parentheses list the input data
aandb. - The colon opens a block—the function body, shifted by 4 spaces.
After a function definition, PEP 8, the commonly accepted rules for formatting Python code, asks for two blank lines.
Parameters and arguments
The parentheses after the name hold the parameters—names that receive values when the function is called. In the call add(2, 3), the arguments 2 and 3 go into the parameters a and b in order. The number of arguments must match the number of parameters.
Interviewers like to ask how a parameter differs from an argument. A parameter is a name in the function definition, and an argument is a value passed in the call.
The word return
The word return ends the function and returns the value of the expression after it. An expression is code that produces a value, such as a + b. The call itself is replaced by the returned value, so it can be passed to print.
The function add prints nothing by itself. It returns a value to the place of the call, and the value is printed by the print that holds the call.
When the body runs
The def line only creates the function, and its body runs on every call. The file runs from top to bottom, so the definition must stand above the first call. If you call a function before the interpreter has reached it, the program stops at that call with a NameError.
This is where the recipe analogy stops working. A cook can look at any page of a cookbook, even the last one. The interpreter knows only the functions whose def lines it has already run.
Click "Next step" and watch when the interpreter enters the body of add and on which line the program stops.
1def add(a, b):2 return a + b3 4 5print("start")6print(add(2, 3))7print(double(4))8 9 10def double(x):11 return x + x
The def line creates the function add, but its body does not run yet.
The program printed start and 5, then stopped with the error NameError: name 'double' is not defined. The lines below the seventh did not run, so the function double was never created. Move its definition above the call, and the program prints 8 as well.
Order of evaluation
Numbers work with the operators +, - and *; * means multiplication. As in math, * goes before + and -, and parentheses change that order. A function call can also be an argument of another call. Then the inner call runs first, and its result goes into the outer one.
print(1 + 2 * 3) # 7
print((1 + 2) * 3) # 9
print(add(add(1, 2), 3)) # 6
print(add(1, 2) * 10) # 30Let's take the last two lines apart:
- In the third line
add(1, 2)returns3, thenadd(3, 3)returns6, and only6is printed. - In the fourth line the call also runs first:
add(1, 2)gives3, and only then3 * 10gives30.
Common mistake
The function body is written without an indent:
def add(a, b):
return a + b # ✗IndentationError: expected an indented block after function definition on line 1After a line ending in a colon, the interpreter expects a block shifted to the right. But the next line starts at the first character. The check of the file finds the error, so the program runs none of its lines. The fix is to shift return a + b by 4 spaces, as in the first example.
Key points
defwith a name, parameters and a colon creates a function, and its body runs on every call.- A parameter is a name in the definition, an argument is a value in the call.
returngives back a value, and it takes the place of the call.- The definition must stand above the first call: otherwise the program stops at the call with a
NameError.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.