Passing function as an argument to another function thus creating a Higher-Order Function

suggest change
func addTwo(item: Int) -> Int {
    return item + 2
}

let addTwoToMe = addTwo

// `math` and `arrayToBeProcessed` are defined in Assigning a function to a variable.
print(math.performOperation(inputArray: arrayToBeProcessed, operation: addTwoToMe))
[3, 5, 7, 9, 11, 13, 10, 8, 6, 4, 102]

Similarly the above could be achieved using a closure

// passing the closure directly to the function as param
print(math.performOperation(inputArray: arrayToBeProcessed, operation: { $0 + 2 }))

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents