- Kotlin for Enterprise Applications using Java EE
- Raghavendra Rao K
- 183字
- 2021-06-10 18:49:20
Default arguments
Default values to the arguments is a nice way for an API to evolve. If we already have a function with two arguments and we need a third argument, we can make the transition easy by making use of default values to arguments. The existing code still works and, at the same time, we can invoke a function with new arguments. Default values to arguments make the API simple and expressive.
Let's write an example to provide a default value to an argument to the function.
Consider the code for 6e_DefaultArguments.kts:
fun welcome(name: String, msg:String = "Hey") {
println("$msg $name")
}
welcome("Bob", "hello")
welcome("Berry")
In this case, we are not providing the second argument, msg, to the welcome() function. It therefore uses a default message instead. The output is as follows:
As shown in the preceding example, a default value to an argument can be specified with an = symbol in the argument declaration. In this case, if we don't pass the second argument while invoking the function, a default value will be provided.
- Learning C# by Developing Games with Unity 2020
- 小創客玩轉圖形化編程
- HTML5游戲開發案例教程
- Learn Swift by Building Applications
- JavaScript by Example
- Spring Boot企業級項目開發實戰
- 學習正則表達式
- HTML+CSS+JavaScript網頁設計從入門到精通 (清華社"視頻大講堂"大系·網絡開發視頻大講堂)
- Flask Web開發:基于Python的Web應用開發實戰(第2版)
- Oracle 12c從入門到精通(視頻教學超值版)
- IBM RUP參考與認證指南
- 游戲設計的底層邏輯
- HikariCP數據庫連接池實戰
- Manage Your SAP Projects with SAP Activate
- Processing開發實戰