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

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.

主站蜘蛛池模板: 庆阳市| 渑池县| 体育| 邓州市| 盐城市| 敖汉旗| 宝应县| 喀什市| 苍溪县| 双鸭山市| 林甸县| 甘德县| 仁怀市| 涡阳县| 中西区| 崇义县| 北辰区| 兴仁县| 大安市| 渝北区| 宜城市| 台东市| 巩义市| 张家口市| 阿克陶县| 曲阜市| 丁青县| 东辽县| 汤原县| 南城县| 绵竹市| 江华| 阿坝| 南平市| 岳池县| 邢台县| 闻喜县| 大石桥市| 永修县| 福贡县| 平凉市|