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

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.

主站蜘蛛池模板: 苍溪县| 西林县| 河池市| 河源市| 徐汇区| 饶平县| 平遥县| 石泉县| 西青区| 绥阳县| 班戈县| 勃利县| 永康市| 海阳市| 桂东县| 全州县| 德格县| 竹溪县| 通江县| 荣昌县| 益阳市| 威远县| 上蔡县| 台中市| 郎溪县| 慈利县| 蒙城县| 辽宁省| 南澳县| 浙江省| 长岛县| 双牌县| 彭州市| 儋州市| 阿鲁科尔沁旗| 文水县| 岚皋县| 鹿邑县| 陵水| 德江县| 抚顺县|