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

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.

主站蜘蛛池模板: 黔东| 丁青县| 错那县| 天津市| 东阳市| 鹤岗市| 黑河市| 繁昌县| 惠州市| 醴陵市| 辰溪县| 珠海市| 宁化县| 昌黎县| 富源县| 苍山县| 清水河县| 泾源县| 临湘市| 南丰县| 温州市| 澄迈县| 长海县| 忻城县| 绍兴市| 建水县| 建昌县| 漯河市| 汕尾市| 滦南县| 河间市| 莒南县| 剑阁县| 咸阳市| 天津市| 左权县| 文安县| 广西| 桐庐县| 肥东县| 吉林省|