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

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.

主站蜘蛛池模板: 闵行区| 纳雍县| 永平县| 丰城市| 顺平县| 台北县| 禄丰县| 新蔡县| 永吉县| 伊宁县| 揭东县| 宁安市| 南木林县| 荆州市| 晋州市| 沅陵县| 资兴市| 苍南县| 南溪县| 化隆| 紫金县| 台北市| 广宗县| 松滋市| 无棣县| 云阳县| 凤庆县| 五华县| 海丰县| 竹山县| 仙居县| 高邑县| 通州区| 库尔勒市| 景谷| 顺义区| 屯留县| 连云港市| 阳原县| 扶风县| 米泉市|