> For the complete documentation index, see [llms.txt](https://gitbook.existorlive.cn/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.existorlive.cn/kai-fa-yu-yan-xue-xi/swift/7.-bi-bao/1.-bi-bao.md).

# 闭包

**闭包**是自包含的函数代码块，可以在代码中被传递和使用。

Swift 中的闭包与 C 和 Objective-C 中的 **代码块(Block)** 以及其他一些编程语言中的 **匿名函数(Lambda)** 比较相似。

**闭包**可以捕获和存储其所在上下文中任意常量和变量的**引用**。被称为**包裹常量和变量**。

Swift 会为你管理在捕获过程中涉及到的所有内存操作。

闭包采用如下三种形式之一：

* **全局函数**是一个有名字但不会捕获任何值的闭包
* **嵌套函数**是一个有名字并可以捕获其封闭函数域内值的闭包
* **闭包表达式**是一个利用轻量级语法所写的可以捕获其上下文中变量或常量值的匿名闭包

***
