Junior — Middle
Kas saaksite selgitada, mida tähendab 'muutuja püüdmine' programmeerimise kontekstis?
sobes.tech AI
Vastus AI-lt
In programming, the term «variable capture» means that a function or closure retains access to variables from the outer scope, even after that scope has finished executing.
For example, in Go, you can create a function that returns another function that uses a variable from the outer function:
func makeAdder(x int) func(int) int {
return func(y int) int {
return x + y // 'x' is captured by the closure
}
}
func main() {
addFive := makeAdder(5)
fmt.Println(addFive(3)) // Prints 8
}
Here, the inner function «captures» the variable x from the outer function makeAdder. This allows maintaining state between calls and creating more flexible structures.