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

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.

主站蜘蛛池模板: 雷山县| 海口市| 东台市| 闽清县| 驻马店市| 灵璧县| 永和县| 孟连| 田东县| 玉溪市| 庆云县| 拉萨市| 荔浦县| 密山市| 璧山县| 洮南市| 万源市| 金秀| 利川市| 房产| 定安县| 东海县| 荣成市| 维西| 虎林市| 宁波市| 成武县| 嘉义县| 广平县| 沧州市| 平和县| 开封县| 岳西县| 汉阴县| 武山县| 正宁县| 新巴尔虎左旗| 紫云| 庆阳市| 东源县| 建瓯市|