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

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.

主站蜘蛛池模板: 花莲市| 盱眙县| 延安市| 奎屯市| 谢通门县| 深水埗区| 蒙自县| 涞源县| 隆德县| 云梦县| 车致| 信宜市| 南昌市| 西贡区| 静乐县| 湘潭市| 斗六市| 方正县| 灌南县| 青岛市| 河池市| 千阳县| 南宫市| 桓仁| 紫阳县| 寻乌县| 卢湾区| 沈阳市| 遵化市| 大姚县| 安溪县| 图木舒克市| 吉隆县| 尚志市| 罗平县| 金乡县| 万荣县| 汝阳县| 甘德县| 郎溪县| 东莞市|