Last active
June 27, 2018 02:21
-
-
Save aainaj/3fc57ab797968848466ec3b28f0551ca to your computer and use it in GitHub Desktop.
Closures Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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