# Functions

## Functions

A function is a group of statements that exist within a program for the purpose of performing a specific task.  Another way to think about a function is that it gives a name to a concept that you might want to reuse.

* Functions represent an idea.
* Functions should do one thing - and one thing well!

### Procedural Programming

Procedural programming is a programming paradigm based upon the concept of the procedure call.  Using functions, also known as procedures, is an example of procedural programming.

### Benefits of Modularizing a Program with Functions

1. Simpler Code - Code tends to be simpler and easier to understand when it is broken into functions.
2. Code Reuse - Functions reduce duplication of code within a program.
3. Easier to test - When each task is contained in its own function, testing and debugging become simpler.

### Defining a Function

A function contains a header and body.  The header begins with the def keyword, followed by the function’s name, parameter(s) or argument(s) and ends with a colon. &#x20;

|                       |                                                                                                                                                                                |
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| def function\_name(): | <p>The keyword def indicates a function is being defined.</p><p>function\_name must follow variable naming rules.</p><p>List of parameters being passed is in parenthesis.</p> |
| \<indented block>     | Indented code to perform some action.                                                                                                                                          |

### Calling a Function

Calling a function executes the code in the function.  To use a function, you have to call it. If the function returns a value, a call to that function is treated as a value.

When a function is called, the control is is transferred to the function.  When the function is finished, the control is returned to where the function was called. &#x20;

### main Function

The main function is the starting point of our program.

{% code title="main.py" %}

```python
def main():
    print("Hello function world!")

main()
```

{% endcode %}
