Kotlin Default & Named Arguments | Android Introduction #17
In this reading, you will be introduced to default arguments and named arguments.
Default Arguments
One very useful Kotlin possibility is to define default arguments. For example, imagine that you implement a function to open a web browser. However, browsers can be opened in normal or incognito mode, so, in your function definition, you should have a parameter that specifies which mode should be used.
fun openBrowser(url: String, incognitoMode: Boolean) {
println("Opening $url" + if (incognitoMode) " in incognito mode" else "")
// ...
}
// Usage
fun main() {
openBrowser("website1.com", false) // Opening website1.com
openBrowser("website2.com", false) // Opening website2.com
openBrowser("website3.com", true) // Opening website3.com in incognito mode
}
However, in most cases, you do not want this function to specify false for incognito mode. It should be used by default. To make that possible, you should specify the default value after the parameter definition.
fun openBrowser(url: String, incognitoMode: Boolean = false) {
println("Opening $url" + if (incognitoMode) " in incognito mode" else "")
// ...
}
// Usage
fun main() {
openBrowser("website1.com") // Opening website1.com
openBrowser("website2.com") // Opening website2.com
openBrowser("website3.com", true) // Opening website3.com in incognito mode
}
You can specify default arguments for all the positions you want. Here is an example function with default arguments for both its parameters of type String.
fun cheer(how: String = "Hello, ", who: String = "World") {
print("$how $who")
}
fun main() {
cheer() // Hello, World
cheer("Hi ") // Hi World
cheer("Hi ", "Learner") // Hi Learner
}
Although you can specify arguments for whatever parameter positions you want, you might be wondering what you can do if you only want to specify the argument for the who parameter. This parameter is at the second position, so to specify it without specifying the arguments for the previous positions, you need to use named arguments.
Named Arguments
When you call a function inside a parenthesis, you can specify arguments according to the order of the parameters that are associated. That’s the default way of calling a function, but there is also another option: for each argument, you can explicitly specify what parameter is associated. You do that by introducing parameter names and the equal sign in front of a parameter. When you do that, arguments do not need to be in parameter order.
fun cheer(how: String = "Hello, ", who: String = "World") {
print("$how $who")
}
fun main() {
cheer(how = "Hi ") // Hi World
cheer(who = "Learner") // Hello, Learner
cheer(how = "Hi ", who = "Learner") // Hi Learner
cheer(who = "Learner", how = "Hi ") // Hi Learner
}
The primary reason for using named arguments is to improve readability — you explicitly state what the meaning of an argument is when it might not be obvious to the person reading your code. The other reason to use it is when you want to specify arguments for parameters at further positions without specifying arguments for the earlier position. For example, when you call cheer(who = “Learner”), you specify who without specifying how.