Declare a closure with arguments

 Problem

I want to declare a closure that takes one or more parameters and/or returns some result.



 Recipe


// Declare a closure with 2 integer arguments
// returning an Integer
let multiply = {(x:Int,y:Int)->Int in
    return x*y
}

// Call it
let result = multiply(2,5)
print(result)
				

Heard the news?

The Swift Cookbook for Swift 3 is at last here! For iBooks, Kindle and Paperback.
Hurry up - and Check it out! ;-)

 

 Playground