官术网_书友最值得收藏!

Functions

Functions are self-contained blocks of code that perform a specific task.

In Swift, functions are first-class citizens, meaning that they can be stored, passed, and returned. Functions can be curried and defined as higher-order functions that take other functions as their arguments.

Functions in Swift can have multiple input parameters and multiple returns using tuples. Let's look at the following example:

func greet(name: String, day: String) ->String { 
return "Hello \(name), today is \(day)"
}

greet(name: "Francois", day:"Saturday")

Functions can have variadic parameters. Consider the following example:

// Variable number of arguments in functions - Variadic Parameters 
func sumOf(numbers: Int...) -> (Int, Int) {
var sum = 0
var counter = 0
for number in numbers {
sum += number
counter += 1
}
return (sum, counter)
}

sumOf()
sumOf(numbers: 7, 9, 45)

Functions can have in-out parameters. Consider the following example:

func swapTwoInts ( a: inout Int, b: inout Int) { 
let temporaryA = a
a = b
b = temporaryA
}

The in-out parameters are not favorable in functional Swift as they mutate states and make functions impure.

In Swift, we can define nested functions. The following example presents a function named add nested inside another function. Nested functions can access the data in scope of their parent function. In this example, the add function has access to the y variable:

func returnTwenty() ->Int { 
var y = 10
func add() {
y += 10
}
add()
return y
}

returnTwenty()

In Swift, functions can return other functions. In the following example, the makeIncrementer function returns a function that receives an Int value and returns an Int value (Int ->Int):

func makeIncrementer() -> ((Int) ->Int) { 
func addOne(number: Int) ->Int {
return 1 + number
}
return addOne
}

var increment = makeIncrementer()
increment(7)
主站蜘蛛池模板: 吉林省| 白河县| 榕江县| 岫岩| 仁怀市| 清苑县| 濮阳县| 渭南市| 河间市| 乌兰浩特市| 万载县| 吉林省| 龙陵县| 云林县| 衡山县| 平定县| 宁远县| 南和县| 阿城市| 乌兰察布市| 汶川县| 额济纳旗| 株洲县| 兴仁县| 怀安县| 甘南县| 柳河县| 庐江县| 凤冈县| 渑池县| 阿拉善右旗| 武穴市| 昌宁县| 隆德县| 隆林| 肇庆市| 辽阳县| 涟水县| 清水河县| 宁明县| 新宁县|