closure in programming

tags: learning go programming

content

“a closure is a function that references variables from outside its own function body” A Tour of Go

func mutate(y int) func(int) int {
 return func(x int) int {
  y += x
  return y
 }
}

in a function like this, returned func is using y, which is outside of its own function body.

or in python, more commonly:

def outer_func(i: int):
	def inner_func():  # inner_func is a closure
		return i + 1   # refering i from outside its own function body
	return inner_func()

up

down