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

# Python Function

A Python function is a first-class object that encapsulates a block of parameterized statements. It is defined using the `def` keyword, executes within its own local lexical scope upon invocation, and yields a value to the caller via the `return` statement, implicitly evaluating to `None` if no return is specified.

## Base Syntax

Functions are declared with the `def` keyword, followed by an identifier, a parenthesized parameter list, and a colon. The function body must be indented.

```python theme={"dark"}
def function_name(param1: int, param2: str = "default") -> bool:
    """Optional docstring for metadata."""
    # Lexical block / Function body
    return True
```

## Parameter and Argument Mechanics

Python supports highly flexible parameter binding mechanisms. Arguments can be passed positionally or by keyword.

* **Default Parameters:** Assigned at function definition time, not at execution time. (Note: Using mutable objects like `list` or `dict` as defaults results in the same object being shared across all calls).
* **Arbitrary Positional Arguments (`*args`):** Captures excess positional arguments into a `tuple`.
* **Arbitrary Keyword Arguments (`**kwargs`):** Captures excess keyword arguments into a `dict`.

### Parameter Modifiers (Python 3.8+)

You can enforce how arguments are passed using special syntax markers `/` and `*`.

```python theme={"dark"}
def strict_function(pos_only, /, standard, *, kw_only):
    pass


# pos_only: Must be passed by position.

# standard: Can be passed by position or keyword.

# kw_only: Must be passed by keyword.
```

## Return Mechanics

The `return` statement terminates function execution and passes an object back to the caller.

```python theme={"dark"}
def return_multiple():
    return 1, 2, 3  # Implicitly packs values into a tuple: (1, 2, 3)

def implicit_return():
    x = 5
    # Implicitly returns None
```

## Scope and Namespaces (LEGB Rule)

When a variable is referenced inside a function, Python resolves the name using the **LEGB** rule, searching in this exact order:

1. **L**ocal: Names assigned within the function.
2. **E**nclosing: Names in the local scope of any enclosing functions (closures).
3. **G**lobal: Names assigned at the top-level of the module.
4. **B**uilt-in: Names preassigned in the built-in names module.

To modify variables outside the local scope, Python provides specific keywords:

* `global`: Binds a local identifier to the module-level namespace.
* `nonlocal`: Binds a local identifier to the nearest enclosing lexical scope (used in nested functions/closures).

```python theme={"dark"}
x = 10
def outer():
    y = 20
    def inner():
        global x
        nonlocal y
        x += 1  # Modifies global x
        y += 1  # Modifies enclosing y
    inner()
```

## First-Class Object Characteristics

Functions in Python are instances of `types.FunctionType`. They possess attributes (like `__name__` and `__doc__`) and can be treated as data. This allows for higher-order functions:

* Assigned to variables.
* Passed as arguments to other functions.
* Returned from other functions.

```python theme={"dark"}
def execute_callback(func, value):
    return func(value) # Executing a passed function
```

## Lambda Functions

Python supports anonymous, inline functions via the `lambda` keyword. Lambdas are restricted to a single expression and cannot contain statements or annotations. The result of the expression is implicitly returned.

```python theme={"dark"}

# Syntax: lambda parameters: expression
multiply = lambda x, y: x * y
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Python Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
