How to Think Like a Computer Scientist: Learning with Python 2nd Edition/Functions

= Functions =

Definitions and use
In the context of programming, a function is a named sequence of statements that performs a desired operation. This operation is specified in a function definition. In Python, the syntax for a function definition is:

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. The list of parameters specifies what information, if any, you have to provide in order to use the new function.

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 first of several compound statements we will see, all of which have the same pattern:


 * 1) A header, 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.

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

The first couple of functions we are going to write have no parameters, so the syntax looks like this:

This function is named new_line. The empty parentheses indicate that it has no parameters. Its body contains only a single statement, which outputs a newline character. (That's what happens when you use a print command without any arguments.)

Defining a new function does not make the function run. To do that we need a function call. Function calls contain the name of the function being executed followed by a list of values, called arguments, which are assigned to the parameters in the function definition. Our first examples have an empty parameter list, so the function calls do not take any arguments. Notice, however, that the parentheses are required in the function call:

The output of this program is:

The extra space between the two lines is a result of the new_line function call. What if we wanted more space between the lines? We could call the same function repeatedly:

Or we could write a new function named three_lines that prints three new lines:

This function contains three statements, all of which are indented by four spaces. Since the next statement is not indented, Python knows that it is not part of the function.

You should notice a few things about this program:


 * You can call the same procedure repeatedly. In fact, it is quite common and useful to do so.
 * You can have one function call another function; in this case three_lines calls new_line.

So far, it may not be clear why it is worth the trouble to create all of these new functions. Actually, there are a lot of reasons, but this example demonstrates two:


 * 1) Creating a new function gives you an opportunity to name a group of statements. Functions can simplify a program by hiding a complex computation behind a single command and by using English words in place of arcane code.
 * 2) Creating a new function can make a program smaller by eliminating repetitive code. For example, a short way to print nine consecutive new lines is to call <tt>three_lines</tt> three times.

Pulling together the code fragments from the previous section into a script named <tt>tryme1.py</tt>, the whole program looks like this:

This program contains two function definitions: <tt>new_line</tt> and <tt>three_lines</tt>. Function definitions get executed just like other statements, but the effect is to create the new function. The statements inside the function do not get executed until the function is called, and the function definition generates no output.

As you might expect, you have to create a function before you can execute it. In other words, the function definition has to be executed before the first time it is called.

Flow of execution
In order to ensure that a function is defined before its first use, you have to know the order in which statements are executed, which is called the flow of execution.

Execution always begins at the first statement of the program. Statements are executed one at a time, in order from top to bottom.

Function definitions do not alter the flow of execution of the program, but remember that statements inside the function are not executed until the function is called. Although it is not common, you can define one function inside another. In this case, the inner definition isn't executed until the outer function is called.

Function calls are like a detour in the flow of execution. Instead of going to the next statement, the flow jumps to the first line of the called function, executes all the statements there, and then comes back to pick up where it left off.

That sounds simple enough, until you remember that one function can call another. While in the middle of one function, the program might have to execute the statements in another function. But while executing that new function, the program might have to execute yet another function!

Fortunately, Python is adept at keeping track of where it is, so each time a function completes, the program picks up where it left off in the function that called it. When it gets to the end of the program, it terminates.

What's the moral of this sordid tale? When you read a program, don't read from top to bottom. Instead, follow the flow of execution.

Parameters, arguments, and the <tt>import</tt> statement
Most functions require arguments, values that control how the function does its job. For example, if you want to find the absolute value of a number, you have to indicate what the number is. Python has a built-in function for computing the absolute value:

In this example, the arguments to the <tt>abs</tt> function are 5 and -5.

Some functions take more than one argument. For example the built-in function <tt>pow</tt> takes two arguments, the base and the exponent. Inside the function, the values that are passed get assigned to variables called parameters.

Another built-in function that takes more than one argument is <tt>max</tt>.

<tt>max</tt> can be sent any number of arguments, separated by commas, and will return the maximum value sent. The arguments can be either simple values or expressions. In the last example, 503 is returned, since it is larger than 33, 125, and 1.

Here is an example of a user-defined function that has a parameter:

This function takes a single argument and assigns it to the parameter named <tt>param</tt>. The value of the parameter (at this point we have no idea what it will be) is printed twice, followed by a newline. The name <tt>param</tt> was chosen to reinforce the idea that it is a parameter, but in general, you will want to choose a name for your parameters that describes their use in the function.

The interactive Python shell provides us with a convenient way to test our functions. We can use the import statement to bring the functions we have defined in a script into the interpreter session. To see how this works, assume the <tt>print_twice</tt> function is defined in a script named <tt>chap03.py</tt>. We can now test it interactively by importing it into our Python shell session:

In a function call, the value of the argument is assigned to the corresponding parameter in the function definition. In effect, it is if <tt>param = 'Spam'</tt> is executed when <tt>print_twice('Spam')</tt> is called, <tt>param = 5</tt> in <tt>print_twice(5)</tt>, and <tt>param = 3.14159</tt> in <tt>print_twice(3.14159)</tt>.

Any type of argument that can be printed can be sent to <tt>print_twice</tt> In the first function call, the argument is a string. In the second, it's an integer. In the third, it's a <tt>float</tt>.

As with built-in functions, we can use an expression as an argument for <tt>print_twice</tt>:

<tt>'Spam'*4</tt> is first evaluated to <tt>'SpamSpamSpamSpam'</tt>, which is then passed as an argument to <tt>print_twice</tt>.

Composition
Just as with mathematical functions, Python functions can be composed, meaning that you use the result of one function as the input to another.

In the first example, <tt>abs(-7)</tt> evaluates to 7, which then becomes the argument to <tt>print_twice</tt>. In the second example we have two levels of composition, since <tt>abs(-11)</tt> is first evaluated to 11 before <tt>max(3, 1, 11, 7)</tt> is evaluated to 11 and <tt>print_twice(11)</tt> then displays the result.

We can also use a variable as an argument:

Notice something very important here. The name of the variable we pass as an argument (<tt>sval</tt>) has nothing to do with the name of the parameter (<tt>param</tt>). Again, it is as if <tt>param = sval</tt> is executed when <tt>print_twice(sval)</tt> is called. It doesn't matter what the value was named in the caller, in <tt>print_twice</tt> it's name is <tt>param</tt>.

Variables and parameters are local
When you create a local variable inside a function, it only exists inside the function, and you cannot use it outside. For example:

This function takes two arguments, concatenates them, and then prints the result twice. We can call the function with two strings:

When <tt>cat_twice</tt> terminates, the variable <tt>cat</tt> is destroyed. If we try to print it, we get an error:

Parameters are also local. For example, outside the function <tt>print_twice</tt>, there is no such thing as <tt>param</tt>. If you try to use it, Python will complain.

Stack diagrams
To keep track of which variables can be used where, it is sometimes useful to draw a stack diagram. Like state diagrams, stack diagrams show the value of each variable, but they also show the function to which each variable belongs.

Each function is represented by a frame. A frame is a box with the name of a function beside it and the parameters and variables of the function inside it. The stack diagram for the previous example looks like this:

The order of the stack shows the flow of execution. <tt>print_twice</tt> was called by <tt>cat_twice</tt>, and <tt>cat_twice</tt> was called by <tt>__main__</tt>, which is a special name for the topmost function. When you create a variable outside of any function, it belongs to <tt>__main__</tt>.

Each parameter refers to the same value as its corresponding argument. So, <tt>part1</tt> has the same value as <tt>chant1</tt>, <tt>part2</tt> has the same value as <tt>chant2</tt>, and <tt>param</tt> has the same value as <tt>cat</tt>.

If an error occurs during a function call, Python prints the name of the function, and the name of the function that called it, and the name of the function that called that, all the way back to the top most function.

To see how this works, create a Python script named <tt>tryme2.py</tt> that looks like this:

We've added the statement, <tt>print cat</tt> inside the <tt>print_twice</tt> function, but <tt>cat</tt> is not defined there. Running this script will produce an error message like this:

This list of functions is called a traceback. It tells you what program file the error occurred in, and what line, and what functions were executing at the time. It also shows the line of code that caused the error.

Notice the similarity between the traceback and the stack diagram. It's not a coincidence. In fact, another common name for a traceback is a stack trace.

Exercises
<ol> <li>Using a text editor, create a Python script named <tt>tryme3.py</tt>. Write a function in this file called <tt>nine_lines</tt> that uses <tt>three_lines</tt> to print nine blank lines. Now add a function named <tt>clear_screen</tt> that prints out twenty-five blank lines. The last line of your program should be a call to <tt>clear_screen</tt>.</li> <li>Move the last line of <tt>tryme3.py</tt> to the top of the program, so the function call to <tt>clear_screen</tt> appears before the function definition. Run the program and record what error message you get. Can you state a rule about function definitions and function calls which describes where they can appear relative to each other in a program?</li> <li>Starting with a working version of <tt>tryme3.py</tt>, move the definition of <tt>new_line</tt> after the definition of <tt>three_lines</tt>. Record what happens when you run this program. Now move the definition of <tt>new_line</tt> below a call to <tt>three_lines</tt>. Explain how this is an example of the rule you stated in the previous exercise.</li> <li> Fill in the body of the function definition for <tt>cat_n_times</tt> so that it will print the string, s, n times:

Save this function in a script named <tt>import_test.py</tt>. Now at a unix prompt, make sure you are in the same directory where the <tt>import_test.py</tt> is located ( <tt>ls</tt> should show <tt>import_test.py</tt>). Start a Python shell and try the following:

If all is well, your session should work the same as this one. Experiment with other calls to <tt>cat_n_times</tt> until you feel comfortable with how it works. </li></ol>