`Swift`中，一个标准的`函数`创建是这样的：

``````func greet(person: String, day: String) -> String {
return "Hello \(person), today is \(day)."
}
greet(person: "Bob", day: "Tuesday")
``````

``````func greet(_ person: String, day: String) -> String {
return "Hello \(person), today is \(day)."
}
greet("Bob", day: "Tuesday")
``````

``````func greet(person: String, _ day: String) -> String {
return "Hello \(person), today is \(day)."
}
greet(person: "Bob","Tuesday")
``````

``````func greet(_ person: String, on day: String) -> String {
return "Hello \(person), today is \(day)."
}
greet("John", on: "Wednesday")
``````

``````func calculateStatistics(scores: [Int]) -> (min: Int, max: Int, sum: Int) {
var min = scores[0]
var max = scores[0]
var sum = 0

for score in scores {
if score > max {
max = score
} else if score < min {
min = score
}
sum += score
}

return (min, max, sum)
}
let statistics = calculateStatistics(scores: [5, 3, 100, 3, 9])
print(statistics.sum)
print(statistics.2)
``````

``````func returnFifteen() -> Int {
var y = 10
y += 5
}
return y
}
returnFifteen()
``````

`Swift`中的函数，是遵循`first-class`原则，也是一等公民，跟其他数据类型一样享受同样的待遇，当然也可以作为一个函数的返回值，比如：

``````func makeIncrementer() -> ((Int) -> Int) {
func addOne(number: Int) -> Int {
return 1 + number
}
}
var increment = makeIncrementer()
increment(7)
``````

``````func hasAnyMatches(list: [Int], condition: (Int) -> Bool) -> Bool {
for item in list {
if condition(item) {
return true
}
}
return false
}
func lessThanTen(number: Int) -> Bool {
return number < 10
}
var numbers = [20, 19, 7, 12]
hasAnyMatches(list: numbers, condition: lessThanTen)
``````

``````var numbers = [20, 19, 7, 12]
``````

``````func square(number: Int) -> Int{
return number * number
}
``````

``````numbers.map(square)
``````

``````[400, 361, 49, 144]
``````

``````numbers.map({ (number: Int) -> Int in
return number * number
})
``````

``````numbers.map({number in number * number})
``````

``````numbers.map({\$0 * \$0})
``````

``````numbers.sorted { \$0 > \$1 }
``````

``````[20, 19, 12, 7]
``````