Encapsulating code with functions
Functions are useful for gathering similar pieces of code in one place. Consider the following mathematical function:

The Python equivalent is as follows:
def f(x):
return 2*x + 1In Figure 1.4 Anatomy of a function the elements of a function block are explained.
- The keyword
deftells Python we are defining a function. fis the name of the function.xis the argument, or input of the function.- What is after
returnis called the output of the function.

Figure 1.4: Anatomy of a function
Once the function is defined, it can be called using the following code:
f(2) # 5 f(1) # 3