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

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.

主站蜘蛛池模板: 长武县| 龙岩市| 灵石县| 剑阁县| 隆安县| 博乐市| 太原市| 新沂市| 东丽区| 宁陵县| 巨鹿县| 新竹市| 阳东县| 图们市| 新巴尔虎左旗| 黄冈市| 通河县| 吉安县| 资中县| 宜昌市| 石河子市| 宜黄县| 平凉市| 广昌县| 得荣县| 喀什市| 太保市| 郑州市| 柏乡县| 公主岭市| 宁海县| 毕节市| 麻江县| 边坝县| 郁南县| 左权县| 榕江县| 邹平县| 邵武市| 东海县| 灌南县|