- 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.
- ASP.NET Web API:Build RESTful web applications and services on the .NET framework
- 小程序?qū)崙?zhàn)視頻課:微信小程序開發(fā)全案精講
- Building Modern Web Applications Using Angular
- WebAssembly實戰(zhàn)
- Python 深度學習
- Java Web開發(fā)技術(shù)教程
- Python數(shù)據(jù)分析從0到1
- Node.js全程實例
- Getting Started with React Native
- 基于ARM Cortex-M4F內(nèi)核的MSP432 MCU開發(fā)實踐
- Scala編程(第5版)
- 從零開始學Android開發(fā)
- Android移動應(yīng)用開發(fā)項目教程
- SwiftUI極簡開發(fā)
- Angular Design Patterns