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

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Functions[edit | edit source]

Definitions and use[edit | edit source]

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 three_lines three times.

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

This program contains two function definitions: new_line and three_lines. 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[edit | edit source]

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 import statement[edit | edit source]

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 abs function are 5 and -5.

Some functions take more than one argument. For example the built-in function pow 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 max.

max 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 param. 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 param 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 print_twice function is defined in a script named chap03.py. 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 param = 'Spam' is executed when print_twice('Spam') is called, param = 5 in print_twice(5), and param = 3.14159 in print_twice(3.14159).

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

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

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

Composition[edit | edit source]

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, abs(-7) evaluates to 7, which then becomes the argument to print_twice. In the second example we have two levels of composition, since abs(-11) is first evaluated to 11 before max(3, 1, 11, 7) is evaluated to 11 and print_twice(11) 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 (sval) has nothing to do with the name of the parameter (param). Again, it is as if param = sval is executed when print_twice(sval) is called. It doesn't matter what the value was named in the caller, in print_twice it's name is param.

Variables and parameters are local[edit | edit source]

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 cat_twice terminates, the variable cat is destroyed. If we try to print it, we get an error:

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

Stack diagrams[edit | edit source]

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:

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

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

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 tryme2.py that looks like this:

We've added the statement, print cat inside the print_twice function, but cat 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.

Glossary[edit | edit source]

Exercises[edit | edit source]

  1. Using a text editor, create a Python script named tryme3.py . Write a function in this file called nine_lines that uses three_lines to print nine blank lines. Now add a function named clear_screen that prints out twenty-five blank lines. The last line of your program should be a call to clear_screen.
  2. Move the last line of tryme3.py to the top of the program, so the function call to clear_screen 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?
  3. Starting with a working version of tryme3.py , move the definition of new_line after the definition of three_lines. Record what happens when you run this program. Now move the definition of new_line below a call to three_lines(). Explain how this is an example of the rule you stated in the previous exercise.
  4. Fill in the body of the function definition for cat_n_times so that it will print the string, s, n times:

    Save this function in a script named import_test.py. Now at a unix prompt, make sure you are in the same directory where the import_test.py is located ( ls should show import_test.py). 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 cat_n_times until you feel comfortable with how it works.