Published using Google Docs
Functions
Updated automatically every 5 minutes

READ this document instead of 6.1 in our textbook. [We have copied parts of  6.1 in our textbook (How To Think Like a Computer Scientist - Learning with Python: Interactive Edition by Jeffrey Elkner, Allen B. Downey, and Chris Meyer) below,  and rewritten parts of it with examples more related to what we are doing. Chapter 6.1 in our textbook includes topics we have not covered yet. Read this document instead of 6.1 in our textbook]

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 syntax for a function definition is:

def name( parameters ):

    statements

You can make up any names you want for the functions you create, except that you can’t use a name that is a Python keyword, and the names must follow the rules for legal identifiers that were given previously. The parameters specify what information, if any, you have to provide in order to use the new function. Another way to say this is that the parameters specify what the function needs to do its work.

There can be any number of statements inside the function, 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 the same amount – 4 spaces is the Python standard – from the header line.

In a function definition, the keyword in the header is def, which is followed by the name of the function and some parameters enclosed in parentheses. The parameter list may be empty, or it may contain any number of parameters separated from one another by commas. In either case, the parentheses are required.

We need to say a bit more about the parameters. In the definition, the parameter list is more specifically known as the formal parameters. This list of names describes those things that the function will need to receive from the user of the function. When you use a function, you provide values to the formal parameters.

The figure below shows this relationship. A function needs certain information to do its work. These values, often called arguments or actual parameters, are passed to the function by the user.

../_images/blackboxproc.png

This type of diagram is often called a black-box diagram because it only states the requirements from the perspective of the user. The user must know the name of the function and what arguments need to be passed. The details of how the function works are hidden inside the “black-box”.

Here is an example. Consider the following code:

When you run this code you get the following output:

Notice there is a lot of repetition! Lines 8, 10 and 12 are exactly the same. Lines 7, 9 and 11 have some similarities.

We have three people and for each one we would like to say hello to them and then also say “It is nice to know you”. We could write a function that does all of this and allows us to give it a different name each time. We will name the function welcome, and we would like to call welcome with each of the three names: Mary, Xiaobai, and Bala.

Here is a program with a function that captures this idea:

If you run this program you get the same output as before.

The function is defined on line 8, is named welcome and it has one parameter named name. Name is a placeholder for the actual name you give the function or “pass” to the function. The complete function is on lines 8 through 10. The lines of code for the function are indented 4 spaces. On line 13 we call the function and pass one argument “Mary”. The parameter name takes the value “Mary”, so when we print on line 9 we print “Hello Mary”. On line 14 we call the function a second time and pass one argument “Xiaobai”. The parameter name takes the value “Xiaobai” this time, so when we print on line 9 we print “Hello Xiaobai”. On line 15 we call the function one more time and pass one argument “Bala”. The parameter name takes the value “Bala” this time, so when we print on line 9 we print “Hello Bala”.

A function is only executed when it is called. When this program is run we start at line 1 and execute line by line. There is nothing to execute on lines 1 through 7. On line 8 we note there is a function on lines 8-10 but we do not execute the function, we just note it is there. The first line of code to execute is on line 13. In order to execute line 13 we call the welcome function and execute lines 8-10, passing “Mary”. Once that is complete, line 13 is done. Next we execute line 14, which also calls the welcome function and executes lines 8-10, this time passing “Xiaobai”. Once the function is complete, line 14 is done. Next we execute line 15, which also calls the welcome function and executes lines 8-10, this time passing “Bala”. Once the function is complete, then line 15 is done and the program is done executing.

Here is another way to write this program.

This program has the same function welcome and produces the same output.

The difference is it has one more line, line 12 has been added. And lines 13-15 are indented.

The line “If __name__ == ‘__main__’:

is an UGLY line. For now just understand that is a special line in python that indicates to start the program on the next line. And that each line of code below this line is indented.

When this program executes it starts on line 1 goes line through line, it sees the function on lines 8-10, and it sees on line 12 the special line. It then executes lines 13-15, each calling the welcome function with a different name.

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. Function calls contain the name of the function to be executed followed by a list of values in parentheses, called arguments, which are assigned to the parameters in the function definition. So in the program above, we call the function welcome one lines 13-15, and pass different strings on each line.

Once we’ve defined a function, we can call it as often as we like and its statements will be executed each time we call it. In this case, we called the function welcome three different times.