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

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.

主站蜘蛛池模板: 普陀区| 万源市| 澳门| 景宁| 墨竹工卡县| 盈江县| 江源县| 桃园县| 禹城市| 嵩明县| 探索| 富裕县| 沂南县| 陇西县| 德惠市| 孟州市| 手游| 泗阳县| 大足县| 阳东县| 浦县| 拉萨市| 鹿泉市| 富蕴县| 佛教| 塘沽区| 万荣县| 印江| 商都县| 启东市| 长沙县| 宁南县| 泸水县| 蓝山县| 上栗县| 秦皇岛市| 玉屏| 灵山县| 乌鲁木齐市| 保靖县| 泊头市|