16
Closures
A closure is an anonymous function that can “close over” variables from an outer function. This is the origin of the term closure. The closed-over variables are called free variables because they are free from the scope of the closure. Like any function, closures execute code, have parameters, and return values.
Closures are a popular feature in many languages, such as Java and C++. In other languages, such as Java, closures are called lambdas.
You may have to choose between using a closure or a standard function. Here are the advantages to closures.
Closures are convenient when there is a single reference to the function.
Closures are ideal as first-class citizens. You can treat closures as function parameters, return values, or ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access