Chapter 16. Function Basics
In Part III, we studied basic procedural statements in Python. Here, we’ll move on to explore a set of additional statements and expressions that we can use to create functions of our own.
In simple terms, a function is a package of code invoked by name. It labels and groups a set of statements so they can be run more than once in a program. A function also can compute a result value, and lets us specify parameters that serve as inputs and may differ each time the function’s code is run. Wrapping an operation in a function makes it a generally useful tool, which we can apply in a variety of contexts.
On a more pragmatic level, functions are the alternative to programming by cutting and pasting—rather than having multiple redundant copies of an operation’s code, we can factor it into a single function. In so doing, we reduce our future work radically: if the operation must be changed later, we have only one copy to update in the function, not many scattered throughout the program.
Functions are also the most basic program structure Python provides for maximizing code reuse, and lead us to the larger notions of program design. As you’ll see, functions let us split complex systems into manageable parts. By implementing each part as a function, we make it both reusable and easier to code.
Table 16-1 abstractly previews the function-related tools we’ll study in this part of the book—a set that includes call expressions, two ways to make functions (def and ...