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

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.

主站蜘蛛池模板: 台南市| 思茅市| 奈曼旗| 敦煌市| 灵台县| 莒南县| 洛川县| 霍邱县| 易门县| 通州市| 凤台县| 博白县| 永顺县| 开封市| 新民市| 青川县| 遂平县| 叙永县| 德令哈市| 永昌县| 兴安盟| 永清县| 上饶县| 随州市| 广州市| 隆昌县| 博白县| 巩留县| 堆龙德庆县| 乐安县| 宝坻区| 延长县| 南漳县| 东乌珠穆沁旗| 汉阴县| 芷江| 阳朔县| 清丰县| 梁河县| 宜州市| 九龙坡区|