In this lesson we show you the most important idea in programming: defining your own functions! Functions let your program become shorter, better-organized, easier to read, easier to debug, and more reusable. In later lessons we will see other benefits like recursion.
Defining a Function
We'll start with an example. Remember the function abs? It takes one argument (a number x), and returns its absolute value (which is x when x ≥ 0, and -x when x < 0). The way to define this function in Python is:
def abs(x): # define a function named 'abs' of one argument named 'x'
if x >= 0: # function body starts here
return x
else:
return -x # function body ends here(Click here to test this code in the console.)
The Two Parts of a Function Definition
The first line in a function definition specifies the name of the function and the arguments of the function. It should look like
def «function-name»(«argument-list»):
In the example above, abs was the function name, and the argument list was x. It is called an argument list because there can be multiple arguments such as x, y, z; it's also possible to have zero arguments, i.e. an empty list. The arguments are the input(s) to the function.
Everything after the first line of a function definition is the body of the function. The body is an indented block of code. Whenever the function is called, the body is executed on those arguments/inputs. Finally, when we reach this line in the function body,
return «value»
the function stops running and returns «value» as its output. That is, the return value is the output of the function. As the abs example shows, the body may contain several return statements; but only the first one executed has an effect since the function stops executing afterwards.
Example: Defining and Calling a Function
The following program contains a function definition and some other statements that are executed after the function is defined.
We see that the function name is square, there is just one argument x, and that the function body consists of just one line return x*x. Then outside of the function we have two commands, which call the function a total of three times.
- When the first command is executed, Python must evaluate
square(10).- Python compares the list of inputs
(10)to the argument list(x). So, it executes the function bodyreturn x*xwhile remembering thatxequals10. Thusx*xyields100, which is printed.
- Python compares the list of inputs
- When the second command is executed, Python must evaluate
square(square(2)).- The inner part
square(2)is evaluated first. We temporarily setxequal to2and run the function body, which returns4. - Then the outer expression is evaluated; since
square(2)gave4, we are now callingsquare(4). This returns16, which is printed.
- The inner part
As usual, you may want to see what this looks like in the Python3 Visualizer.
Four Common Mistakes
One common mistake you can make in defining a function is to forget the return statement.
As you can see, if no return statement is encountered in the body, then the function gives None by default. So, if you are failing some exercise because a function is returning None, the problem is often that some function inputs are not causing a return statement to be executed.
![]() | You may also intentionally omit a return statement. This only makes sense if your function has some side effect other than its return value, for example printing some output:
|
Another common mistake is forgetting to indent the code, resulting in an IndentationError.
As we saw in lesson 2, calling with too few or too many arguments causes an error.
Finally, if you make a typo when defining or calling the function, you will get an error saying that the function is undefined.
Try it Yourself
Two or More Arguments
The functions above only took one argument, but a function can be designed to take any number of arguments. For example, you have been calling input() with 0 arguments (and you'll define a function with zero arguments in lesson 15A).
Here's an example with two arguments, about geometry. Suppose we need a function to compute the area of a triangle, given the length of its base and its height. The formula for the area is
area = base × height / 2
Then in Python, we can define a function triangleArea as follows:
As you can see, the only change is that we replaced def «function-name»(«argument1») with def «function-name»(«argument1», «argument2»).
In the last exercise we ask you to write your own function of two arguments, to compute the perimeter of a rectangle. (The perimeter is the total length of all sides.) In a rectangle with a given width and height, its perimeter is given by the following formula:
perimeter = width + height + width + height
Functions Calling Functions
Functions are the building blocks of well-built large programs: you can do the same task twice without writing out the same code twice, and you can re-use solutions to common tasks. Here is an example of building one function and using it inside of another one.
You are now ready for the next lesson!



