6.1. Functions

In Python, a function is a named sequence of statements that belong together. Their primary purpose is to help us organize programs into chunks that match how we think about the solution to the problem.

The above function, called main, draws a sqaure. The function has two parts. The first part, the function definition, is lines 3 through 7, and specifies what the function does when it is executed. The second part, the function call is line 9 and it executes the function.

There can be any number of statements inside the function definition, but they have to be indented from the def. In the examples in this book, we will use the standard indentation of four spaces. Function definitions are the second of several compound statements we will see, all of which have the same pattern:

  1. A header line which begins with a keyword and ends with a colon.

  2. A body consisting of one or more Python statements, each indented 4 spaces from the header line.

We’ve already seen the for loop which follows this pattern.

In a function definition, the keyword in the header is def, which is followed by the name of the function and the return type. We will go into the purpose of the return type in a later chapter.

Defining a new function does not make the function run. To do that we need a function call. This is also known as a function invocation. We’ve already seen how to call some built-in functions like print, range and int. So in the last line of the program, we call the function to actually draw the square.

It is worth noting that a program with functions does not neccessarily execute its lines of code from top to bottom. Because the function code is only executed when it is called, the program execution can jump around. To see this in action, use CodeLens to step through the animation of the following program:

Check your understanding

    func-1-1: What is a function in Python?

  • A named sequence of statements.
  • Yes, a function is a named sequence of statements.
  • Any sequence of statements.
  • While functions contain sequences of statements, not all sequences of statements are considered functions.
  • A mathematical expression that calculates a value.
  • While some functions do calculate values, the python idea of a function is slightly different from the mathematical idea of a function in that not all functions calculate values. Consider, for example, the turtle functions in this section. They made the turtle draw a specific shape, rather than calculating a value.
  • A statement of the form x = 5 + 4.
  • This statement is called an assignment statement. It assigns the value on the right (9), to the name on the left (x).

    func-1-2: What is one main purpose of a function?

  • To improve the speed of execution
  • Functions have little effect on how fast the program runs.
  • To help the programmer organize programs into chunks that match how they think about the solution to the problem.
  • While functions are not required, they help the programmer better think about the solution by organizing pieces of the solution into logical chunks that can be reused.
  • All Python programs must be written using functions
  • In the first several chapters, you have seen many examples of Python programs written without the use of functions. While writing and using functions is desirable and essential for good programming style as your programs get longer, it is not required.
  • To calculate values.
  • Not all functions calculate values.
You have attempted of activities on this page
6. Functions"> 6.2. Function Parameters">Next Section - 6.2. Function Parameters