Search code examples
swiftfunctionfirst-class-functions

“Functions are a first-class type” in swift?


The little knowledge I have about first-class functions is that it supports passing functions as arguments and we can also return them as the values in another function ... I am very new in Swift programming language. Can anyone please elaborate it with an example?


Solution

  • A very simple example to demonstrate this behaviour:

    func functionA() {
        println("Hello by functionA")
    }
    
    func executeFunction(function: () -> ()) {
        function()
    }
    
    executeFunction(functionA)