Recall & Review
beginner
What does it mean that functions are types in Swift?
In Swift, functions can be treated like any other type, such as Int or String. This means you can store functions in variables, pass them as arguments, and return them from other functions.
Click to reveal answer
beginner
How do you declare a variable that holds a function taking two Ints and returning an Int?
You declare it like this:
var operation: (Int, Int) -> Int. This means operation is a variable that stores a function which takes two Int values and returns an Int.Click to reveal answer
intermediate
Can you pass a function as a parameter to another function in Swift? How?
Yes! You specify the parameter type as a function type. For example:
func performOperation(_ a: Int, _ b: Int, operation: (Int, Int) -> Int) -> Int. Then you can call operation(a, b) inside the function.Click to reveal answer
intermediate
What is the syntax to return a function from another function in Swift?
You specify the return type as a function type. For example:
func makeAdder(_ x: Int) -> (Int) -> Int returns a function that takes an Int and returns an Int.Click to reveal answer
beginner
Why is treating functions as types useful in Swift?
It allows you to write flexible and reusable code. You can pass behavior around, customize actions, and create powerful abstractions like callbacks and event handlers.
Click to reveal answer
How do you declare a variable that holds a function taking a String and returning Void in Swift?
✗ Incorrect
The correct syntax uses parentheses around parameters and an arrow: (String) -> Void.
Which of these is a valid way to pass a function as a parameter in Swift?
✗ Incorrect
Option D correctly declares a parameter that is a function taking two Ints and returning an Int.
What is the return type of this function?
func makeMultiplier() -> (Int) -> Int✗ Incorrect
The return type (Int) -> Int means the function returns another function that takes an Int and returns an Int.
Can you assign a function to a variable in Swift?
✗ Incorrect
Functions are types, so you can assign them to variables if the types match.
What is the benefit of using functions as types?
✗ Incorrect
Using functions as types lets you pass behavior around, making code more flexible.
Explain how you can use functions as types to pass a function as an argument in Swift.
Think about how you write the parameter type and how you use it inside the function.
You got /3 concepts.
Describe how returning a function from another function works and why it might be useful.
Consider how you can create customized functions on the fly.
You got /3 concepts.