Skip to content

Instantly share code, notes, and snippets.

@aainaj
Last active June 27, 2018 02:21
Show Gist options
  • Save aainaj/3fc57ab797968848466ec3b28f0551ca to your computer and use it in GitHub Desktop.
Save aainaj/3fc57ab797968848466ec3b28f0551ca to your computer and use it in GitHub Desktop.
Closures Examples
//: Playground - Closures
// Closure take no parameter and return nothing
let sayHello: () -> Void = {
print("Hello")
}
sayHello()
// Closure take one parameter and return 1 parameter
let value: (Int) -> Int = { (value1) in
return value1
}
print(value(5))
// Closure take two parameter and return 1 parameter
let add: (Int, Int) -> Int = { (value1, value2) in
return value1 + value2
}
print(add(5, 4))
// Closure take two parameter and return String parameter
let addValues: (Int, Int) -> String = { (value1, value2) -> String in
return String("Sum is: \(value1 + value2)")
}
print(addValues(5, 4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment