- 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.
- Mastering Spring MVC 4
- Flash CS6中文版應(yīng)用教程(第三版)
- Microsoft Dynamics GP 2013 Reporting, Second Edition
- QGIS:Becoming a GIS Power User
- Yocto for Raspberry Pi
- Python算法從菜鳥到達(dá)人
- Scala編程實戰(zhàn)(原書第2版)
- Python:Deeper Insights into Machine Learning
- 分布式架構(gòu)原理與實踐
- 零基礎(chǔ)學(xué)C++(升級版)
- HTML5 Canvas核心技術(shù):圖形、動畫與游戲開發(fā)
- Building Apple Watch Projects
- Mastering Node.js
- 軟件測試項目實戰(zhàn)之功能測試篇
- TensorFlow.NET實戰(zhàn)