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

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.

主站蜘蛛池模板: 高要市| 韶山市| 华池县| 怀仁县| 洱源县| 乐安县| 崇礼县| SHOW| 雷州市| 福清市| 太保市| 界首市| 醴陵市| 米泉市| 临湘市| 武山县| 多伦县| 和田市| 射阳县| 资源县| 昆明市| 乐亭县| 大邑县| 青龙| 巧家县| 马公市| 武强县| 江陵县| 公主岭市| 漳平市| 宝鸡市| 陆川县| 延寿县| 紫阳县| 山东省| 昆明市| 宁国市| 堆龙德庆县| 临邑县| 五河县| 包头市|