- Kotlin for Enterprise Applications using Java EE
- Raghavendra Rao K
- 170字
- 2021-06-10 18:49:20
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.
- JavaScript高效圖形編程
- 前端跨界開發(fā)指南:JavaScript工具庫原理解析與實戰(zhàn)
- Building a Recommendation Engine with Scala
- Full-Stack React Projects
- Linux操作系統(tǒng)基礎(chǔ)案例教程
- Java編程技術(shù)與項目實戰(zhàn)(第2版)
- Flutter跨平臺開發(fā)入門與實戰(zhàn)
- 動手學(xué)數(shù)據(jù)結(jié)構(gòu)與算法
- RESTful Java Web Services(Second Edition)
- 實戰(zhàn)Java高并發(fā)程序設(shè)計(第2版)
- C指針原理揭秘:基于底層實現(xiàn)機制
- Mastering OAuth 2.0
- Java從入門到精通(視頻實戰(zhàn)版)
- 征服C指針(第2版)
- 一覽眾山小:ASP.NET Web開發(fā)修行實錄