## Functions
### Advantages of functions
1. Reduce duplication of code
2. Induce reusability of code
3. Using functions reduces size of code
4. Can call the functions any number of times
## Types of Functions
1. Built-in Functions (Python libraries)
Prebuilt in python
eg: abs(), len(), range(), max(), min(), str(), type(),bool() etc
```
max(80,90,70)
output: 90
max is a built in function
```
2. Functions defined within modules
functions which we can import from modules
eg: ceil(x), floor(x), log(x), sin(x), modf(x) etc
```
from math import sqrt
math.sqrt(25)
output: 5
sqrt() is a function which we imported from math
```
3. User Defined function
Syntax:
```
def function_name(parameter):
"Docstrings"
#block of statement(s)
function_name(parameter1) //caling the function
```
eg:
```
def lambda(a,b)
"This function takes 2 inputs and add them"
return a+b
print(lambda(10,20))
output: 30
```
### Defining functions in python
1. it uses a keyword "**def**"
2. a function name is unique and must not be repeated
3. **Parameters** are assigned by which we can ask the user for input to perform any function
4. a colon is used at the end functions name
5. **Docstrings** are included which tell us about the usability of the function
6. a **return** statement is used when a value is returned by the function
### How a function works
1. Execution begins from the first statement of the program
2. If a fuction definition is found python executes only the name of function and skips the code inside the function
3. When later on the when python sees the function being called it lets the header take the control and then the function's body code is executed
4. Function's bode code is executed till return statement or the last line of code
### Function Parameters
There are 2 types of parameters
1. Formal Parameters
2. Actual Parameters
eg:
```
def sum(x,y)
return x + y
a = 10
b = 20
print(sum(a,b))
```
x and y are formal parameters
a and b are actual parameters