# Closures

A closure is when you enclose a function inside of another, like so:

```lua
function a(var)
    local x = 5
    local function b()
       print(x, var) 
    end
    return b
 end
```

When you create a closure, all of the variables are accessible in its parent's scope. In this case, b has access to a's scope. This provides a way to access and manipulate variables inside a function after its parent returns. The variables that the closure can access are called upvalues. The variables `x` and `var` are both upvalues to function `b` in this case.

Take this example, a simple function to increment a value, which is inaccessible without the closure:

```lua
function counter()
    local i = 0
    return function()
        i = i + 1
        return i
    end
end
```

Here we define a function that returns an anonymous function (a function that has no name or identifier) which will increment the upvalue `i` and return the value back to us.

Each time we call `counter`, it generates a new counter for us, it can be called any amount of times and all the counters will have different values. Each time the returned function from counter is called (the function that increments), it will only change that counter, no others.

```lua
function counter()
    local i = 0
    return function()
        i = i + 1
        return i
    end
end

local counter_1 = counter() -- now counter_1 holds a unique function to increment the upvalue
local counter_2 = counter() -- counter_2 holds a new unique incrementer
counter_1() -- increment our first counter, i is now 1
counter_2() counter_2() counter_2() -- increment counter_2 3 times, i is 3, counter_1 is unaffected

print(counter_1(), counter_2()) -- Output: 2 4
```

For the print, the counters must be incremented again so we can get the value, since each time our incrementers run they return the variable after incrementing it.


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://stigmax.gitbook.io/lua-guide/concepts/closures.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
