Python Functions
In Python, a function is a named block of code that can take a wide variety of input parameters (or none at all) and return some form of output back to the code that called the function to begin with. It represents a key concept in programming sometimes referred to as DRY, which stands for Don’t Repeat Yourself. The idea behind DRY is that if you perform some particular operations in your code multiple times, you can simply create a function to reuse that block of code anywhere you need it instead of duplicating effort by typing it each time.
Python offers two types of functions: built-in functions that are part of the standard library and functions you create yourself. The standard library includes a huge number of functions you can use in your program, like print(), many of which you have already been introduced to in Chapter 3. Building your own functions is how you construct capabilities that are not already present within the Python language.
To define a function in Python, you use the keyword def, a name for the function, a set of parentheses enclosing any arguments you want to pass to the function, and a colon at the end. The name of a function must follow these rules:
Must not start with a number
Must not be a reserved Python word, a built-in function (for example, print(), input(), type()), or a name that has already been used as a function or variable
Can be any combination of the A–Z, a–z, 0–9 and the underscore (_) and dash (-)
The following is an example of an incredibly simple function that could be entered into the interactive Python interpreter:
Python 3.8.1 (v3.8.1:1b293b6006, Dec 18 2019, 14:08:53) [Clang 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> def devnet(): '''prints simple function''' print('Simple function') >>> devnet() Simple function
This function prints out the string “Simple function” any time you call it with devnet(). Notice the indented portion that begins on the next line after the colon. Python expects this indented portion to contain all the code that makes up the function. Keep in mind that whitespace matters in Python. The three single quotation marks that appear on the first line of the indented text of the function are called a docstring and can be used to describe what the function does.
As shown in the following example, you can use the built-in Python function help() to learn what a function does and any methods that can be used:
>>> help(devnet) Help on function devnet in module __main__: devnet() prints simple function