Understanding Python Functions
Tags: python, beginners, tutorial, programming
Published: 2023-10-25 20:52:25

Well, let's dive straight into it. 

Functions are blocks of code written to perform a particular task. Well, this definition is quite simple and straightforward. This simply means, your functions should do only one thing.

## Defining Functions In Python
To create a function, python provides a special keyword, `def`. This keyword is used in combination with a user defined name you give to the function. Let's take a look
```python
def name_of_function():
   # function block
```

Every function should have a name, but not every name is allowed because some are reserved by Python itself.

Inside the function block is where you write what the function should do when called.

### Parameters
When writing a function, you can define parameters that you want the function to take in.
> A parameter is used to represent data you want as input to the function

Parameters are defined inside the parenthesis in the function definition.

Example, let's say you want to write a function that adds two numbers, we will name the function, add.
```python
def add(a, b):
    print(a + b)
```
In the above example, the function add has two parameters, a and b which we are using to represent the two numbers we will pass in.


### Calling Functions
Once a function is written, then it must be used somewhere. I mean, I'm not sure you will write a function without the intention of using it 😅

To use a function, you call the function with its name.
```python
def add(a, b):
    print(a + b)

add(2, 4)
```

From the above example, we called the function with it's name and passed in the parameters it requires.
Suppose the function had no parameters, you will just call it with its name and the brackets. `add()`

### Parameters and Arguments - Revisited
In the above section, I explained what parameters are. Reiterating, a parameter is used to represent data you want to pass as input to a function.

When calling a function that has some parameters, the actual values you pass in to that function are called the `Arguments`.

Let's go over the example again
```python
def add(a, b):
    print(a + b)

add(2, 4)
```

When we were defining the functions, we passed a, b inside the brackets, those are called the **function parameters**.

The real/actual values we pass to the function when calling it are what we call the **arguments**. In our "add" example, "2" and "4" are the arguments.

Arguments are also called **"Actual Parameters"** and the parameters specified in the function definition are called "Formal Parameters"

> **Parameters are specified in the function definition and arguments are the values passed into the function when calling it**


### Types of Functions
There are two types of functions in Python: 
- **_User-Defined Functions_**: These are the functions you create, like the "add" function we defined. You choose the name and define what it does.

- **_Built-In Functions_**: Python comes with a collection of pre-made functions that perform common tasks. These are functions like `print()`, `len()`, and `input()`, which are available for you to use without defining them yourself. Built-in functions save you time and effort, as they are already part of the Python language.

To conclude, functions are really powerful and understanding it will really be important in your programming journey.
Here are some importance of functions:
- Improving code reusability:  Once a function is defined, it can be used over and over and over again.


- Code Organisation: It helps to divide the large programs into small groups to read the code and debug it faster and better.

- Functions can be shared and used by other programmers.