100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Swift

Closures in Swift

Understand closures as self-contained blocks of functionality that capture and store references to surrounding variables.

Functions & ClosuresIntermediate9 min readJul 8, 2026
Analogies

Introduction

Closures are self-contained blocks of functionality that can be passed around and used in your code, similar to functions. Unlike functions, closures can be written without a name and can capture and store references to constants and variables from the surrounding context in which they are defined. This capturing behavior is what makes closures powerful for callbacks, completion handlers, and functional-style operations.

🏏

Cricket analogy: A fielding coach's quick hand-signal instruction to the slip cordon is like a closure — it doesn't need a formal name, and it 'captures' the current field placement from the moment it's given, later used as a callback when the bowler actually delivers.

Syntax

swift
{ (parameters) -> ReturnType in
    statements
}

// Assigned to a constant
let multiply: (Int, Int) -> Int = { (a: Int, b: Int) -> Int in
    return a * b
}

Explanation

A closure expression has a parameter list, a return type, and a body, separated by the in keyword. Swift's syntax is progressively simplified in common cases: types can often be inferred from context so you can drop the parameter and return types; single-expression closures can omit the return keyword because Swift infers it automatically; and for very short closures you can use shorthand argument names $0, $1, and so on, instead of naming parameters. When a closure is the last argument to a function, Swift also lets you write it using trailing closure syntax, placing the closure body outside the parentheses.

🏏

Cricket analogy: Full field-instructions to a bowler would spell out (line, length) -> Outcome in, but experienced captains infer it from context, drop the formal wording, and just point using shorthand like 'there, there' ($0, $1) — and when it's the last instruction before the over, they say it trailing, after handing over the ball.

Example

swift
let numbers = [5, 2, 8, 1]

// Full closure syntax
let sortedFull = numbers.sorted(by: { (a: Int, b: Int) -> Bool in
    return a < b
})

// Trailing closure + type inference + implicit return
let sortedShort = numbers.sorted { a, b in a < b }

// Shorthand argument names
let sortedShorthand = numbers.sorted { $0 < $1 }

print(sortedFull)
print(sortedShorthand)

Output

swift
[1, 2, 5, 8]
[1, 2, 5, 8]

Key Takeaways

  • A closure has the form { (parameters) -> ReturnType in statements }.
  • Closures can capture and store references to variables and constants from their surrounding context.
  • Swift allows progressively shorter syntax: type inference, implicit returns, and shorthand argument names like $0.
  • Trailing closure syntax moves a final closure argument outside the parentheses.
  • Closures are used extensively for callbacks and functional operations like sorted(by:).

Practice what you learned

Was this page helpful?

Topics covered

#Swift#SwiftProgrammingStudyNotes#Programming#ClosuresInSwift#Closures#Syntax#Explanation#Example#Functions#StudyNotes#SkillVeris