> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Functions

> Functions are essential for writing clean, modular shell scripts, covering best practices for naming, defining, and styling functions in Bash.

## Overview

Functions are essential for writing clean, modular shell scripts. They help you:

* Reuse code and avoid duplication
* Improve readability and maintainability
* Handle complex tasks in a structured way

This guide covers best practices for naming, defining, and styling functions in Bash.

## Naming Conventions

Adopt consistent naming to make your functions self-documenting. Follow these rules:

| Guideline                     | Recommendation            | Example           |
| ----------------------------- | ------------------------- | ----------------- |
| Lowercase names               | Enhances uniformity       | `calculate_area`  |
| Descriptive identifiers       | Conveys purpose instantly | `backup_database` |
| Underscores between words     | Improves readability      | `get_user_info`   |
| No single-letter or CamelCase | Prevents ambiguity        | `process_files`   |

<Callout icon="lightbulb" color="#1CB2FE">
  Descriptive, lowercase names with underscores help developers and automation tools understand your code at a glance.
</Callout>

## Defining Functions

Use this standard syntax for declaring functions:

```bash theme={null}
function_name() {
    # function body
}
```

Key style guidelines:

1. Include parentheses `()` after the name.
2. Place the opening brace `{` on the same line, preceded by one space.
3. Align the closing brace `}` with the function declaration (no extra indentation).

### Example Definitions

```bash theme={null}
calculate_area() {
    local radius=$1
    echo "Area: $(( 3 * radius * radius ))"
}

get_name() {
    local user_id=$1
    # Retrieve the user’s name from a data source
    echo "User Name for ID $user_id"
}

clone_repo() {
    local repo_url=$1
    git clone "$repo_url"
}
```

<Callout icon="triangle-alert" color="#FF6B6B">
  Misplacing braces or omitting parentheses leads to syntax errors and unexpected behavior.
</Callout>

## Advanced Function Patterns

In upcoming sections, you’ll learn how to:

* Define functions without the `function` keyword
* Use `local` variables to prevent global namespace pollution
* Return status codes and handle errors gracefully
* Parse command-line options using `getopts`

## Further Reading

* [Advanced Bash-Scripting Guide](https://tldp.org/LDP/abs/html/)
* [Bash Reference Manual](https://www.gnu.org/software/bash/manual/bash.html)
* [getopts Bash Builtin](https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-getopts)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/advanced-bash-scripting/module/06219b3e-dc63-404c-a9df-3ea035628308/lesson/0cf92c1d-2397-4d91-8070-2136a7cc610b" />
</CardGroup>
