Declare a Closure With Multiple Arguments in Swift

In this short Swift code example, you will learn how to declare a Closure with multiple arguments in Swift.

  • Declare a Closure with two arguments
  • Call a Closure

Declare and Call a Closure

Example #1

let fullName = { (firstName:String, lastName:String)->String in
    return firstName + " " + lastName
}

//Call Closure
let myFullName = fullName("Sergey", "Kargopolov")
print("My full name is \(myFullName)")

Example #2

let sumOf = { (numberOne:Int, numberTwo:Int) -> Int in 
    return numberOne + numberTwo    
}

//Call Closure
let total = sumOf(10, 15)
print("Total is \(total)")

For more Swift code examples and tutorials, please check the Swift Code Examples page on this website.


Leave a Reply

Your email address will not be published.