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

Named arguments

We can use a named argument to invoke a function and pass the arguments by naming them. Since the arguments are named, the order doesn't need to be maintained.

Let's take a look at the following example. Consider the code for 6f_NamedArguments.kts:

fun welcome(name: String, msg:String = "Hey") {
println("$msg $name")
}
welcome(msg = "How are you", name = "Mark")

The output is as follows:

We can also mix named arguments with arguments without a name. Consider the code for 6g_NamedArguments.kts:

fun welcome(name: String, msg:String = "Hey") {
println("$msg $name")
}
welcome("Joseph", msg = "Hi")

The output is as follows:

In this case, we invoked the function with one positional argument with the value Joseph and one argument named msg. When we are dealing with multiple arguments in a method, we can choose to send some arguments positionally by maintaining the order and to send others as named arguments. The language gives us the flexibility to invoke functions with type safety.

主站蜘蛛池模板: 左云县| 类乌齐县| 德清县| 图木舒克市| 亚东县| 随州市| 徐汇区| 黔江区| 甘肃省| 涞水县| 西乌珠穆沁旗| 宜丰县| 陆良县| 大化| 台北县| 大关县| 沈阳市| 甘泉县| 荆门市| 湘潭县| 长泰县| 凤城市| 漳平市| 博客| 长葛市| 灵台县| 榆树市| 久治县| 开封市| 拜城县| 靖州| 桂东县| 内江市| 神木县| 澄城县| 德州市| 岳池县| 延津县| 朔州市| 昌黎县| 肥乡县|