> For the complete documentation index, see [llms.txt](https://www.pythonclassroom.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://www.pythonclassroom.com/functions.md).

# 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 %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://www.pythonclassroom.com/functions.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
