Swift Language Function as first class citizens in Swift Passing function as an argument to another function, thus creating a Higher-Order Function

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

func multiply2(item: Int)-> Int
{
    return (item + 2)
}


let multiply2ToMe = multiply2

// passing the function directly to the function as param
print(math.performOperation(inputArray: arrayToBeProcessed, operation: multiply2ToMe))

Output:

[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 }))


Got any Swift Language Question?