Complete the code to declare a function variable that adds two numbers.
let add: (Int, Int) -> Int = [1]In Swift, you can assign a closure to a variable. The correct syntax for a closure that adds two numbers is { (a, b) in a + b }.
Complete the code to call the function stored in the variable.
let result = add[1]3, 5)
To call a function or closure in Swift, use parentheses with arguments inside, like add(3, 5).
Fix the error in the function assignment by choosing the correct syntax.
let multiply: (Int, Int) -> Int = [1](a, b) in { return a * b }
When assigning a closure to a variable, use curly braces {} around the closure body, not the func keyword.
Fill both blanks to create a function that takes another function as a parameter and calls it.
func performOperation(_ operation: (Int, Int) -> Int, _ a: Int, _ b: Int) -> Int {
return operation[1]a, b[2]
}To call the function parameter operation, use parentheses around the arguments: operation(a, b).
Fill all three blanks to create a dictionary that maps strings to functions and call one of them.
let operations: [String: (Int, Int) -> Int] = [
"add": [1],
"subtract": [2]
]
let result = operations["add"][3](10, 5)!The dictionary stores closures for addition and subtraction. To call a function from the dictionary safely, use the forced unwrap operator ! after the call, since the dictionary returns an optional.