> For the complete Mojo documentation index, see [llms.txt](/llms.txt).
> Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

# @parameter

You can add the `@parameter` decorator on a nested function to create a
[parametric closure](#parametric-closure).

:::note

The `@parameter` decorator also works to create compile-time `if` and `for`
statements, but this usage is deprecated in favor of the
[`comptime if`](/docs/manual/metaprogramming/comptime-evaluation/#comptime-if)
and
[`comptime for`](/docs/manual/metaprogramming/comptime-evaluation/#comptime-for)
statements.

:::

## Parametric closure

You can add `@parameter` on a nested function to create a "parametric"
capturing closure. This means you can create a closure function that captures
values from the outer scope (regardless of whether they are variables or
parameters), and then use that closure as a parameter. For example:

```mojo
def use_closure[func: fn(Int) capturing [_] -> Int](num: Int) -> Int:
    return func(num)

def create_closure():
    var x = 1

    @parameter
    def add(i: Int) -> Int:
        return x + i

    var y = use_closure[add](https://mojolang.org/docs/reference/decorators/2.md)
    print(y)

create_closure()
```

```output
3
```

Note the `[_]` in the function type:

```mojo
def use_closure[func: fn(Int) capturing [_] -> Int](num: Int) -> Int:
```

This origin specifier represents the set of origins for the values captured by
the parametric closure. This allows the compiler to correctly extend the
lifetimes of those values. For more information on lifetimes and origins, see
[Lifetimes, origins and references](/docs/manual/values/lifetimes/).

If you leave the `@parameter` decorator off the `add()` function above, you'll
get a compiler error:

```output
invalid call to 'use_closure': failed to infer parameter '__origins__'
```

## Parametric `if` statement

The `@parameter if` syntax is deprecated, please use `comptime if` instead. For
more information, see the section on
[compile-time conditionals](/docs/manual/metaprogramming/comptime-evaluation/#comptime-if).

## Parametric `for` statement

The `@parameter for` syntax is deprecated, please use `comptime for` instead.
For details, see the section on
[compile-time loop unrolling](/docs/manual/metaprogramming/comptime-evaluation/#comptime-for).
