Functions are useful for gathering similar pieces of code in one place. Consider the following mathematical function:
![](https://static.packt-cdn.com/products/9781786463517/graphics/B05511_01_03-1.jpg)
The Python equivalent is as follows:
def f(x): return 2*x + 1
In Figure 1.4 Anatomy of a function the elements of a function block are explained.
- The keyword
def
tells Python we are defining a function. f
is the name of the function.x
is the argument, or input of the function.- What is after
return
is called the output of the function.
![](https://static.packt-cdn.com/products/9781786463517/graphics/Anatomy_of_a_function.jpg)
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