## What is Function?

Functions are simply a set of commands created to do a particular function. For example, the `print()`

function contains certain commands to make the function that allows us to output to the screen.

## Why Functions?

Now you have thought of something like this, is it okay if I don’t use a function? Of course, you can not use it, but after a certain point, when the line of code increases, the complexity increases, and it becomes difficult to control. That’s why we use functions.

Thus, instead of writing code each time for the functions we want to do, we create a function once, and call it and use it whenever we want to do it. Thus, we get rid of excess code complexity.

Briefly Functions;

- Each of them does a certain job.
- We can divide it into two functions that take a parameter or a function that does not.
- The function of functions is to gather complex operations together and enable us to perform these operations in one step.
- By using functions, we can collect operations consisting of one or more steps under a single name.

If we know what functions are and why we use them, let’s learn how to use them.

## Function Usage

There are two types of functions in Python, the first is the functions that come ready with Python, that is, the embedded functions, and the second is the functions we have created. Since functions like `print()`

and `type()`

come with Python, we only call them where we want to use them.

The general usage outline of the functions is as follows.

```
def function_name(parameter1, parameter2):
"""docstring"""
statement(s)
function_name(parameter1, parameter2)
```

We have seen the draft in general, now let’s show it with an example. Let me create a simple function for this right away.

```
def output():
"""Prints `Baransel.dev Python Lessons`"""
print("Baransel.dev Python Lessons")
```

As you can see we have created our function. Now let’s call our function, for this we just write the name of the function;

```
def output():
"""Prints `Baransel.dev Python Lessons`"""
print("Baransel.dev Python Lessons")
output()
# Baransel.dev Python Lessons
```

Since we called it once, it did this function once. The number of times we call it, the more it prints to the screen.

```
def output():
"""Prints `Baransel.dev Python Lessons`"""
print("Baransel.dev Python Lessons")
output()
output()
output()
# Baransel.dev Python Lessons
# Baransel.dev Python Lessons
# Baransel.dev Python Lessons
```

If you wish, let’s write the same function in a different way. I will use the `return`

statement here because we will write functions that will return us results, not just printing to the screen, so we will use `return`

.

`return`

Statement

The `return`

statement allows the function to return any value.

```
def output():
return "Baransel.dev Python Lessons"
print(output())
# Baransel.dev Python Lessons
```

In this way, we have printed the value returned by the function.

We learned functions that do not take `parameters`

in Python. Now let’s write a `function`

that takes `parameters`

and `returns`

a value.

```
def addition(number1,number2):
return number1 + number2
print("Result :", addition(3,4))
# Result: 7
```

In this way, we have written a function that returns the sum of the numbers we entered. Well, can we assign values to these parameters beforehand, of course, let me show you right away;

```
def addition(number1=6,number2=8):
return number1 + number2
print("Result :", addition())
# Result : 14
```

As you can see, we can assign values to the parameters beforehand. So when we call the Function we don’t need to value the parameters. Well, what happens if we give the parameter a value, let’s give it a value and try it right away.

```
def addition(number1=6,number2=8):
return number1 + number2
print("Result :", addition(4,3))
# Result : 7
```

As you can see, this time it returned 7 instead of 14. That is, if we do not give a value when we call the function, Python defaults to the values we previously gave to the parameters.

Until now, we have always given values to the parameters within the function, that is, arguments, so let’s operate on the functions with the values we get from outside.

```
def addition(number1,number2):
return number1 + number2
number1 = int(input("Enter the first number:"))
number2 = int(input("Enter the second number:"))
print("Result :",addition(number1,number2))
# Enter the first number:24
# Enter the second number:37
# Result : 61
```

## Function Within Function

Now, in this application, we will perform to Calculate Standard Deviation application. Let’s briefly explain the Standard Deviation. The standard deviation is the square root of the sum of the squares of the difference of the numbers in a series from the arithmetic mean of the series divided by the number of elements in the series minus one.

To explain a little, to calculate the standard deviation;

- The arithmetic mean of the numbers is calculated.
- Each number is different from the arithmetic mean.
- The square of each of the differences found is calculated.
- The squares of the differences are added up.
- The resulting sum is divided by one less than the number of elements in the series.
- Take the square root of the number found.

For this, we will first calculate the mean of the series with the `meanCalculate()`

function, return the server we found, and use the `standardCalculate()`

function.

```
def meanCalculate(list):
mean = sum(list) / len(list)
return mean
def standardCalculate(list):
standardSquare=[]
for i in range(len(list)):
standardSquare.append((list[i]-meanCalculate(list))**2)
standardDeviation = (sum(standardSquare)/len(list)-1)**0.5
return standardDeviation
list = [22,23,20,14,32,2,40]
print("Standard Deviation:",standardCalculate(list))
# Standard Deviation: 11.249489784348588
```