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?
Afunc example(closure: (Int) -> String) {}
Bfunc example(closure: String -> Int) {}
Cfunc example(closure: Int) -> String {}
Dfunc example(closure: () -> ()) {}
✗ 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?
ATo declare a closure inside a function
BTo write the closure outside the parentheses when it is the last parameter
CTo capture variables inside a closure
DTo convert a closure to a function
✗ 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?
AdoSomething() { print("Hello") }
BdoSomething({ print("Hello") })
CdoSomething(print("Hello"))
DdoSomething(print) { "Hello" }
✗ Incorrect
Trailing closure syntax places the closure outside the parentheses, like doSomething() { ... }.
What does it mean when a closure 'captures' a variable?
AIt copies the variable to a new function
BIt deletes the variable after use
CIt keeps a reference to the variable from its surrounding context
DIt converts the variable to a constant
✗ 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?
ATo store data permanently
BTo make the function run faster
CTo avoid writing functions
DTo customize what the function does with code you provide
✗ 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.
Practice
(1/5)
1.
What is the main purpose of passing a closure as a function parameter in Swift?
easy
A. To allow the function to execute custom code provided by the caller
B. To make the function run faster
C. To prevent the function from returning a value
D. To declare a global variable inside the function
Solution
Step 1: Understand what a closure is
A closure is a block of code that can be passed around and executed later.
Step 2: Understand why closures are passed to functions
Passing a closure lets the function run custom code without changing its own code.
Final Answer:
To allow the function to execute custom code provided by the caller -> Option A
Quick Check:
Closures customize function behavior = A [OK]
Hint: Closures let functions run your code inside them [OK]
Common Mistakes:
Thinking closures speed up the function
Confusing closures with variables
Believing closures stop function returns
2.
Which of the following is the correct syntax to declare a function that takes a closure parameter with no input and no return value?
func performAction(____) { }
easy
A. action: () => Void
B. action: Void -> ()
C. action: () -> () -> Void
D. action: () -> Void
Solution
Step 1: Recall closure syntax in Swift
A closure with no input and no return is written as () -> Void.
Step 2: Match syntax to function parameter
The parameter should be named and typed as action: () -> Void.
Final Answer:
action: () -> Void -> Option D
Quick Check:
Closure type syntax = D [OK]
Hint: Closure type: inputs in (), arrow, then return type [OK]
Common Mistakes:
Using 'Void -> ()' which is invalid
Adding extra arrows like '() -> () -> Void'
Using '=>' which is not Swift syntax
3.
What will be printed when the following Swift code runs?
The function greet takes a closure that returns a String and prints "Greeting: " plus the closure's result.
Step 2: Evaluate the closure passed
The closure returns "Hello, Swift!", so the print statement outputs "Greeting: Hello, Swift!".
Final Answer:
Greeting: Hello, Swift! -> Option C
Quick Check:
Closure returns string printed with prefix = A [OK]
Hint: Closure returns string, function prints with prefix [OK]
Common Mistakes:
Expecting just the closure string without prefix
Confusing closure name with output
Thinking code causes error
4.
Identify the error in this Swift function that takes a closure parameter:
func repeatTask(times: Int, task: () -> Void) {
for _ in 1...times {
task
}
}
medium
A. The closure 'task' is not called with parentheses
B. The loop range should start from 0
C. The closure parameter must return a value
D. The function must return a value
Solution
Step 1: Check how the closure is used inside the loop
The code uses 'task' without parentheses, so the closure is not executed.
Step 2: Correct closure call syntax
To run the closure, it must be called as 'task()' with parentheses.
Final Answer:
The closure 'task' is not called with parentheses -> Option A
Quick Check:
Call closure with () to execute = C [OK]
Hint: Call closures with () to run them [OK]
Common Mistakes:
Forgetting parentheses to call closure
Changing loop range unnecessarily
Expecting closure to return value when void
5.
Given this function that filters numbers using a closure, how would you call it to get only even numbers?
func filterNumbers(_ numbers: [Int], using condition: (Int) -> Bool) -> [Int] {
var result = [Int]()
for number in numbers {
if condition(number) {
result.append(number)
}
}
return result
}
Choose the correct call:
hard
A. filterNumbers([1,2,3,4]) { number in number * 2 }
B. filterNumbers([1,2,3,4]) { $0 % 2 == 0 }
C. filterNumbers([1,2,3,4]) { _ in true }
D. filterNumbers([1,2,3,4]) { $0 > 5 }
Solution
Step 1: Understand the closure parameter
The closure takes an Int and returns Bool to decide if number is included.
Step 2: Identify condition for even numbers
Even numbers satisfy number % 2 == 0, so closure should return true for those.
Step 3: Match correct call
filterNumbers([1,2,3,4]) { $0 % 2 == 0 } uses shorthand $0 % 2 == 0, which returns true for even numbers.