next up previous contents
Next: Scoping: How Python finds Up: Functions Previous: Functions   Contents

Introduction

Functions are an important part of any programming language for two reasons. First, they allow you to reuse code you've written, without the need to copy and modify the code each time you use it. For example, if you are working with a data base, you'll always need to make a connection to the data base, and inform it of the table you want to access. By writing a function to do this, you can dispose of the task with a single line of code in every program that needs to access the data base. An added advantage to using a function for a task like this is that if you ever need to change the type of data base you're using, or if you detect a flaw in the logic you used when you first wrote the function, you can simply edit a single version of the function, and other programs can simply import the corrected version to be instantly updated.

The second reason to use functions is that it allows you to logically isolate the different sub-tasks which invariably emerge when you're working on a program. In the database example, you'd generally need to connect to the database, and then either query the database or make some changes. By writing one function to connect, a second to query and a third to update, you can write the main part of your program very concisely, and study its logic without having to get into the details of the database itself. Debugging such a program becomes much simpler because, once a set of functions has been developed and tested, it's not hard to figure out whether a problem is arising from one of the functions, or from the code that's calling it. This style of programming is known as modular programming, and is generally recognized as a useful way to make writing easy-to-read and maintainable programs.

In Python, functions are just one more type of object. Thus you can assign a function to another name, store them in lists or tuples, pass them to other functions, and so on. But they have one special property which sets them apart from most other Python objects: when you provide them with a list of arguments surrounded by parentheses, they can perform some task, using the arguments as a guide as to what they should do, and, optionally they can return a value, which can be used like any other value of the same type.

We've already seen a number of functions in previous chapters, such as len (Section 2.4.4), which returns the length of a sequence such as a string or a list, and min and max (Section 4.4), which return the minimum or maximum value contained in a sequence. We've also encountered many methods, which are very similar to functions, and defined in basically the same way.

In this chapter we'll examine some of the issues surrounding functions and how to create functions and import them into your programs.


next up previous contents
Next: Scoping: How Python finds Up: Functions Previous: Functions   Contents
Phil Spector 2003-11-12