Swift: Function vs Closure
By somaria
2022-11-08
import Cocoa /* Create a function that takes a string and returns a string. */ func sayHello(name: String) -> String { return "Hello \(name)!" } /* print the result of calling sayHello with "Cocoa" as the argument. */ print(sayHello(name: "Cocoa")) /* Create a closure that takes a string and returns a string. */ let sayHelloClosure = { (name: String) -> String in return "Hello \(name)!" } /* print the result of calling sayHelloClosure with "Cocoa" as the argument. */ print(sayHelloClosure("Closure"))

Output

Hello Cocoa!

Hello Closure!