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 device that groups a set of statements so they can be run more than once in a programâa packaged procedure invoked by name. Functions also can compute a result value and let us specify parameters that serve as function inputs and may differ each time the code is run. Coding an operation as a function makes it a generally useful tool, which we can use in a variety of contexts.
More fundamentally, 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 weâ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 previews
the primary function-related tools weâll study in this part of the bookâa
set that includes call expressions, two ways to make functions (
Get Learning Python, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.