Recall & Review
beginner
What is a closure in Swift?
A closure is a block of code that can be passed around and used in your code. It can capture and store references to variables and constants from the surrounding context.
Click to reveal answer
beginner
How do you pass a closure as a parameter to a function in Swift?
You define the function parameter with a closure type, for example: (Int) -> Int, then you can pass a closure when calling the function.
Click to reveal answer
intermediate
What is the trailing closure syntax in Swift?
If the last parameter of a function is a closure, you can write the closure outside the parentheses when calling the function. This makes the code cleaner and easier to read.
Click to reveal answer
beginner
Why are closures useful as function parameters?
Closures let you customize behavior by passing code to functions. This helps write flexible and reusable code, like sorting or filtering with custom rules.Click to reveal answer
intermediate
What does it mean that closures can capture values?
Closures can remember and use variables from the place where they were created, even if that place no longer exists when the closure runs.
Click to reveal answer
How do you declare a function in Swift that takes a closure parameter which accepts an Int and returns a String?
✗ Incorrect
The closure parameter type is written as (Int) -> String, meaning it takes an Int and returns a String.
What is the trailing closure syntax used for in Swift?
✗ Incorrect
Trailing closure syntax lets you write the closure after the function call parentheses if it is the last parameter.
Which of these is a correct way to call a function with a closure parameter using trailing closure syntax?
✗ Incorrect
Trailing closure syntax places the closure outside the parentheses, like doSomething() { ... }.
What does it mean when a closure 'captures' a variable?
✗ Incorrect
Capturing means the closure keeps a reference to variables from where it was created, so it can use them later.
Why use closures as function parameters?
✗ Incorrect
Closures let you pass custom code to functions, making them flexible and reusable.
Explain how to define and use a closure as a function parameter in Swift.
Think about how you write the function signature and how you call it with a closure.
You got /3 concepts.
Describe what it means that closures capture values and why this is useful.
Imagine a closure remembering something from where it was created.
You got /3 concepts.