The built-in functions are useful but generic. Often, there aren’t built-in functions that do what you want, such as calculate mileage or play a game of cribbage. When you want functions to do these sorts of things, you have to write them yourself.
Because scientific work, international travel, and global communication often involve both Celsius and Fahrenheit temperature scales, it’s helpful to have a quick way to convert between the two systems. Writing a conversion function makes these temperature conversions easier and more reliable, especially when they’re needed repeatedly. It sure would be nice to be able to do this:
| | >>> convert_to_celsius(212) |
| | 100.0 |
| | >>> convert_to_celsius(78.8) |
| | 26.0 |
| | >>> convert_to_celsius(10.4) |
| | -12.0 |
However, the function convert_to_celsius doesn’t exist yet, so instead, you see this (focus only on the last line of the error message for now):
| | >>> convert_to_celsius(212) |
| | Traceback (most recent call last): |
| | File "<python-input-0>", line 1, in <module> |
| | convert_to_celsius(212) |
| | ^^^^^^^^^^^^^^^^^^ |
| | NameError: name 'convert_to_celsius' is not defined |
To fix this, you have to write a function definition that tells Python what to do when the function is called.
You’ll go over the syntax of function definitions soon, but let’s start with an example:
| | >>> def convert_to_celsius(fahrenheit): |
| | ... return (fahrenheit - 32) * 5 / 9 |
| | ... |
The function body is indented. Here, you indent four spaces, as the Python style guide recommends. If you forget to indent, you get an IndentationError:
| | >>> def convert_to_celsius(fahrenheit): |
| | ... return (fahrenheit - 32) * 5 / 9 |
| | ... |
| | File "<python-input-1>", line 2 |
| | return (fahrenheit - 32) * 5 / 9 |
| | ^^^^^^ |
| | IndentationError: expected an indented block after function definition on line 1 |
Now that you’ve defined the function convert_to_celsius, your earlier function calls will work. You can even use the built-in function help on it:
| | >>> help(convert_to_celsius) |
| | Help on function convert_to_celsius in module __main__: |
| | |
| | convert_to_celsius(fahrenheit) |
The output shows the first line of the function definition, which is referred to as the function header. (Later in this chapter, you’ll see how to add more help documentation to a function.)
Here is a quick overview of how Python executes the following code:
| | >>> def convert_to_celsius(fahrenheit): |
| | ... return (fahrenheit - 32) * 5 / 9 |
| | ... |
| | >>> convert_to_celsius(80) |
| | 26.666666666666668 |
Python executes the function definition, which creates the function object (but doesn’t execute it yet).
A function definition introduces a new variable
Python creates a variable named convert_to_celsius whose value is the function definition.
Next, Python executes the function call convert_to_celsius(80). To do this, it assigns 80 to fahrenheit (which is a variable). For the duration of this function call, fahrenheit refers to 80.
The return statement determines the value produced as a result
Python now executes the return statement. The variable fahrenheit refers to 80, so the expression that appears after return is equivalent to (80 - 32) * 5 / 9. When Python evaluates that expression, 26.666666666666668 is produced. Use the word return to tell Python what value to produce as the result of the function call, so the result of calling convert_to_celsius(80) is 26.666666666666668.
Once Python has finished executing the function call, it returns to the place where the function was originally called.
Here is an image showing this sequence:

A function definition is a Python statement. The general form of a function definition is as follows:
| | def function_name(parameters): |
| | body |
A parameter is a variable
The function header (that’s the first line of the function definition) starts with def, followed by the name of the function, then a comma-separated list of parameters within parentheses, and then a colon. A parameter is a variable.
You can’t have two functions with the same name in the same file; it isn’t an error, but if you do it, the second function definition replaces the first one, much like assigning a value to a variable a second time replaces the first value.
Below the function header and indented (four spaces, as per Python’s style guide) is a block of statements called the function body. The function body must contain at least one statement.
Most function definitions will include a return statement that, when executed, ends the function and produces a value. The general form of a return statement is as follows:
| | return expression |
When Python executes a return statement, it evaluates the expression and then produces the result of that expression as the result of the function call.