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

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.

主站蜘蛛池模板: 岳西县| 海城市| 大宁县| 普兰县| 寿阳县| 高州市| 博湖县| 浪卡子县| 云浮市| 栾城县| 日土县| 沂南县| 门头沟区| 古蔺县| 高密市| 鄱阳县| 化德县| 浪卡子县| 定南县| 娄烦县| 皋兰县| 那坡县| 晋江市| 肥城市| 普定县| 通许县| 林州市| 清徐县| 石门县| 惠安县| 虞城县| 阜平县| 吴江市| 武定县| 大兴区| 保靖县| 洛浦县| 新乐市| 阿克陶县| 霞浦县| 五寨县|