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?
Avar action: (String) -> Void
Bvar action: String -> Void
Cvar action: (String) => Void
Dvar action: function(String) -> Void
✗ 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?
Afunc run(operation: Int, Int) -> Int {}
Bfunc run(operation: (Int, Int)) {}
Cfunc run(operation: function) {}
Dfunc run(operation: (Int, Int) -> Int) {}
✗ 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
AA function that takes an Int and returns an Int
BAn Int value
CA tuple of Ints
DVoid
✗ 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?
AOnly if the function returns Void
BNo, functions cannot be assigned to variables
CYes, if the variable type matches the function type
DOnly if the function has no parameters
✗ Incorrect
Functions are types, so you can assign them to variables if the types match.
What is the benefit of using functions as types?
AIt makes code run faster
BIt allows passing behavior as data
CIt prevents errors automatically
DIt makes variables immutable
✗ 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.
Practice
(1/5)
1. What does it mean when we say "functions are types" in Swift?
easy
A. Functions can be stored in variables and passed around like values.
B. Functions cannot be assigned to variables.
C. Functions must always return an Int type.
D. Functions cannot accept parameters.
Solution
Step 1: Understand the concept of types in Swift
In Swift, types define what kind of values variables can hold.
Step 2: Apply this to functions
Functions have types too, meaning they can be assigned to variables, passed as arguments, or returned from other functions.
Final Answer:
Functions can be stored in variables and passed around like values. -> Option A
Quick Check:
Functions as values = Functions can be stored in variables and passed around like values. [OK]
Hint: Remember: functions behave like any other value in Swift [OK]
Common Mistakes:
Thinking functions can't be assigned to variables
Assuming functions must return Int
Believing functions can't take parameters
2. Which of the following is the correct way to declare a variable that holds a function taking two Ints and returning an Int?
easy
A. var operation: (Int, Int) Int
B. var operation: Int -> Int -> Int
C. var operation: (Int Int) -> Int
D. var operation: (Int, Int) -> Int
Solution
Step 1: Recall Swift function type syntax
Function types are written as (ParameterTypes) -> ReturnType, with parameters separated by commas.
Step 2: Check each option
var operation: (Int, Int) -> Int correctly uses (Int, Int) -> Int. Others have syntax errors or missing arrows.
Final Answer:
var operation: (Int, Int) -> Int -> Option D
Quick Check:
Correct function type syntax = var operation: (Int, Int) -> Int [OK]
Hint: Use (Type1, Type2) -> ReturnType for function types [OK]
Common Mistakes:
Omitting commas between parameter types
Missing arrow '->' in function type
Incorrect parameter grouping
3. What will be the output of the following Swift code?
func add(_ a: Int, _ b: Int) -> Int { a + b }
var mathOperation: (Int, Int) -> Int = add
print(mathOperation(3, 4))
medium
A. 7
B. 34
C. Error: Cannot assign function
D. 0
Solution
Step 1: Understand function assignment
The function 'add' matches the type (Int, Int) -> Int, so it can be assigned to 'mathOperation'.