Introduction to Python Functions!!
Tags: python, programming, machinelearning, datascience
Published: 2021-07-31 16:07:11

<P>
Functions are named blocks of code that are designed to do one specific code. Functions facilitate reusability of code. When you want to do something repeatedly you define a function and call it whenever you need it.
</P>
<h2>Built-In Functions</h2>
<p> 
Python comes with several built-in functions that you can use to create wonderful programs. Here are a few but not limited to built-functions.
</p>
<table style="width:100%">
 <tr>
    <td>print()</td>
    <td>range()</td>
    <td>input()</td>
    <td>list()</td>
  </tr>
  <tr>
    <td>next()</td>
    <td>round()</td>
    <td>max()</td>
    <td>tuple()</td>
  </tr>
  <tr>
    <td>str()</td>
    <td>dic()</td>
    <td>sorted()</td>
    <td>type()</td>
  </tr>
<tr>
    <td>object()</td>
    <td>sum()</td>
    <td>set()</td>
    <td>eval()</td>
  </tr>

</table>
<h2> Creating a function</h2>
<p> In python a function is defined using the <b style="background-color:grey, color:blue">def</b> keyword
for example:
</P>
```py
def greet():
    print("Hello World")
```
<h3> Calling a function</h3>
<p>The above function definition is inert until the the function is triggered or called. when you call the function:
</p>
```py
greet() # Hello World
```
<h2>Arguments</h2>
<p>
Arguments refer to the information that is passed into the function
Arguments are specified after the function name, inside the parenthesis. You can add as many arguments as you want but you have to separate them with a comma. for example:
</p>
```py
def greet(name):
    print( "Hello" +" "+ name)
greet("John") # Hello John
```
<h3>Positional Arguments</h3>
<p>
Because a function can have multiple parameters, a function call may need multiple arguments.You can use positional arguments which need the same order the parameters were written. This is how it works:
</p>
```py
def greet(name , place):
    print( f" Hello {name} from {place}")
greet( John, Uganda) # Hello John from Uganda
```
Now imagine a situation when then name and the place are swapped  when the function is called.
```py
greet(Uganda, John) # Hello Uganda from John
```
<p>
And this quite doesn't make sense.
This means that the first argument in the function call is used as the value of the first parameter and the second argument in the function call is used as the value of the second parameter.
To avoid such such situations from happening when can specify both the arguments and parameters together with the values that they take. These are called <b>Keyword arguments</b>. Here the order of the arguments in the function call doesn't matter as long as the names of the parameters are correct.
</p>
Now we have:
```py
greet(place ="Uganda", name="John") # Hello John from Uganda
```
<h3>Arguments and Parameters</h3>
<p>
A parameter is the variable listed inside the parentheses in the function definition.
An argument is the value that is sent to the function when it is called.
</p>
<h3> Number of Arguments</h3>
<p>By default, a function should be called with correct number of arguments. Should call a function with more or less arguments than it expects it will bring an error.
</p>

 for example:
```py
def greet(name, place):
  print("Hello" + " " + name +" "+ place)

greet("Jack", "Ghana") # Hello Jack from Ghana
```
If you try to call this function with 1 or 3 arguments
```py
greet("Jack")
```
You get this error:
```
Traceback (most recent call last):
  File "demo_function_args_error.py", line 4, in <module>
    greet("Jack")
TypeError: greet() missing 1 required positional argument: 'place'
```
<h3>Default Value Parameter</h3>
<p>
When writing a function, you can define a default value for each parameter. If an argument for a parameter is provided in the function call, Python uses the argument value. If not, it uses the parameter’s default value. So when you define a default value for a parameter, you can exclude the corresponding argument you’d usually write in the function call. 
</p>
```py
def people(country = "Kenya"):
  print("I am from " + country)

people("Sweden")
people("Burundi")
people("Ghana")
people("Nigeria")
```
<h2>Return Values</h3>
To let a function return a value, use the <b>return</b> statement:
```py
def sum(x):
  return 4 + x

print(sum(1)) # 5
print(sum(3)) # 7
print(sum(10)) # 14
```
In conclusion, we have covered:
<ul>
<li>what are functions</li>
<li>Built-in functions</li>
<li>how to define functions</li>
<li>calling functions</li>
<li>Arguments</li>
<li>the difference between arguments and parameters</li>
<li>Return values</li>
Hope you enjoyed reading this article. Thank you for reading.