Use closure as a parameter

 Problem

I want to declare a function that takes a closure as a parameter and then call it.



 Recipe


// Declare a function
// that takes a closure (with 2 integer parameters)
// as an argument

func doSth(action:(Int,Int)->()) {
    // Run the passed 'action'
    // with the parameters: 2, 5
    action(2,5);
}

// Call it
doSth({(x:Int, y:Int)->() in
    print("x * y = \( x * y )")
});
				


 Playground