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

# Defining functions

> Create your own reusable functions

## Your first function

A function is a named block of code that performs a specific task. You define it once, then call it whenever you need that task done.

```python theme={null}
def greet():
    print("Hello, world!")
    print("Welcome to Python!")

# Call the function
greet()
```

## Function syntax

Every function follows this pattern:

```python theme={null}
def function_name():
    # Code goes here
    # Must be indented
    pass
```

Key parts:

* `def` - keyword that creates a function
* Function name followed by parentheses `()`
* Colon `:` to start the function body
* Indented code block (the function body)

## Naming functions

Follow these rules for function names:

* Use lowercase letters
* Separate words with underscores
* Be descriptive about what it does

```python theme={null}
# Good names
def calculate_total():
    pass

def send_email():
    pass

def validate_password():
    pass

# Bad names
def func1():  # Not descriptive
    pass

def Calculate():  # Should be lowercase
    pass
```

## Calling functions

To use a function, call it by name with parentheses:

```python theme={null}
def say_goodbye():
    print("Goodbye!")
    print("See you later!")

# Call it multiple times
say_goodbye()
say_goodbye()
say_goodbye()
```

<Tip>
  The power of functions: write once, use many times. The above code prints 6 lines with just 3 function calls!
</Tip>

## Functions with logic

Functions can contain any Python code:

```python theme={null}
def check_weather():
    temperature = 25
    if temperature > 30:
        print("It's hot!")
    else:
        print("Nice weather!")

# Use the function
check_weather()
```

## Variable scope: Local vs Global

Variables in Python have a "scope" - where they can be accessed and used.

### Local variables

Variables created inside a function only exist within that function:

```python theme={null}
def calculate_price():
    price = 100
    tax = price * 0.1
    print(f"Total: {price + tax}")

calculate_price()  # Total: 110

# This fails - price doesn't exist outside the function
print(price)  # NameError: name 'price' is not defined
```

### Global variables

Variables created outside functions can be accessed anywhere:

```python theme={null}
discount_rate = 0.15  # Global variable

def apply_discount(price):
    discount = price * discount_rate  # Can read global variable
    return price - discount

result = apply_discount(100)
print(result)  # 85.0
```

### Modifying global variables

To change a global variable inside a function, use the `global` keyword:

```python theme={null}
counter = 0  # Global variable

def increment():
    global counter  # Declare we want to modify the global variable
    counter += 1

increment()
increment()
print(counter)  # 2
```

<Warning>
  Avoid using `global` when possible. It makes code harder to understand and debug. Instead, pass values as parameters and return results.
</Warning>

### Best practice: Use parameters and returns

```python theme={null}
# Bad - using global variable
total = 0

def add_to_total(amount):
    global total
    total += amount

# Good - using parameters and return
def add_amounts(current_total, amount):
    return current_total + amount

total = 0
total = add_amounts(total, 10)
total = add_amounts(total, 20)
print(total)  # 30
```

<Tip>
  When a local and global variable have the same name, the local variable "shadows" the global one inside the function. Python always uses the local version first.
</Tip>

## Common mistakes

<AccordionGroup>
  <Accordion title="Forgetting parentheses when calling">
    ```python theme={null}
    # Wrong - this doesn't call the function
    greet

    # Right - parentheses are required
    greet()
    ```
  </Accordion>

  <Accordion title="Forgetting the colon">
    ```python theme={null}
    # Wrong
    def greet()
        print("Hello")

    # Right
    def greet():
        print("Hello")
    ```
  </Accordion>

  <Accordion title="Bad indentation">
    ```python theme={null}
    # Wrong - not indented
    def greet():
    print("Hello")

    # Right - must indent function body
    def greet():
        print("Hello")
    ```
  </Accordion>
</AccordionGroup>

## What's next?

Functions become powerful when they can accept input. Let's learn about parameters!

<Card title="Parameters" icon="arrow-right" href="/functions/parameters">
  Pass data to functions
</Card>
