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

Defining and using variadic functions

Swift enables us to define functions with variadic parameters. A variadic parameter accepts zero or more values of a specified type. Variadic parameters are similar to array parameters but they are more readable and can only be used as the last parameter in multi-parameter functions.

As variadic parameters can accept zero values, we will need to check whether they are empty.

The following example presents a function with variadic parameters of the String type:

func greet(names: String...) { 
for name in names {
print("Greetings, \(name)")
}
}

// To call this function
greet(names: "Josee", "Jorge") // prints twice
greet(names: "Josee ", "Jorge ", "Marcio") // prints three times

The most boring part of the chapter is almost over. Seriously, we needed to master the Swift function/method syntax and we will see the benefits in upcoming chapters. If you speed read or skipped previous sections, it is okay. The upcoming sections are going to be essential for FP so they need more attention.

主站蜘蛛池模板: 洪洞县| 将乐县| 伊金霍洛旗| 襄汾县| 渑池县| 应用必备| 洪泽县| 许昌市| 岳池县| 喀什市| 蛟河市| 阜南县| 平度市| 赤壁市| 会同县| 高要市| 江门市| 张家港市| 红安县| 依兰县| 泊头市| 永泰县| 四子王旗| 阿克苏市| 洛扎县| 南康市| 凉山| 湟源县| 义乌市| 会宁县| 岳池县| 沭阳县| 宁陕县| 靖江市| 沅陵县| 广宗县| 平山县| 团风县| 广安市| 中西区| 凌海市|