Like other programming languages, python provides _pre-defined functions_ ([`print`][print], [`map`][map] etc) that are readily available in the Python language.
Data. known as `parameters`, can be passed to the function by placing them inside the parenthesis. A broader term for parameters is `arguments`. Functions can perform different tasks depending on the value of the parameters.
A function can also return a value using the `return` keyword. The value returned by the function is known as the `return value`. The return value is returned to the caller of the function.
In python, functions are created using the `def` keyword. The function definition is followed by the function name and parenthesis [`()`]. Inside the parenthesis, the parameters are specified, separated by commas. After the close parenthesis, the colon (`:`) is used to separate the function signature from the function body.
The function body is a block of code that is executed when the function is called. The body of the function is indented. The indentation is important because Python relies on it to know where that block of code ends. A value can be returned from the function by using the `return` keyword, which can then be used by the caller of the function.
To call a function, use the function name followed by parenthesis [`()`]. Parameters passed to the function are placed inside the parenthesis, separated by commas.
When the function is called, the parameters are passed to the function. We need to pass values for both the parameters, otherwise a [`TypeError`][type-error] will be raised.
TypeError: add() takes 2 positional arguments but 3 were given
```
## Return Value
The return value is a value that is returned to the caller of the function. Return value can be of any data type. It can be used by caller of the function to perform further operations. If the function does not explicitly return a value, the value `None` is returned.
Assume a program has to perform the following tasks:
* Calculate the area of a circle
* Calculate the area of a rectangle
* Calculate the area of a triangle
We can break down the program into smaller parts.
```python
def circle_area(r):
return 3.14 * r * r
def rectangle_area(length, breadth):
return length * breadth
def triangle_area(base, height):
return 0.5 * base * height
```
Now, we can call the functions in the order we want.
```python
>>> circle_area(2)
12.56
>>> rectangle_area(2, 3)
6
>>> triangle_area(2, 3)
1.5
```
## Scope of Variables
If variable is defined inside a function, then it will be only accessible inside the function. If we want to access the variable outside the function, we need to use the [`global`][global] keyword. [`nonlocal`][nonlocal] keyword is used to access the variable inside a nested function.
```python
def outer():
x = 10
print('Inside function:', x)
```
variable `x` is defined inside the outer function. It is limited to the scope of the outer function only. It will not alter the value of the variable outside the outer function.
```python
>>> x = 30
# regardless of whether we call the function or not, the value of x will be 30
>>> outer()
Inside function: 10
>>> x
30
```
We can access the variable inside the outer function using the `global` keyword.
```python
def outer():
global x
x = 10
print('Inside function:', x)
```
As we have used the `global` keyword, the value of `x` will be changed.
In python, functions can be assigned to variables and passed as arguments to other functions. They can be used as return values. Functions can also be placed into a sequence([`list`][list], [`tuple`][tuple] etc) or as value in a [`dict`][dict]. Functions can be used anywhere than any other object can be used. This is because _functions are [`first class objects`][first class objects]_.