In any programming language, functions are really powerful instruments. Sometimes you don't need to use all the arguments of a function, as some of them may be stored as preselected values. In this topic, you will learn how to do it.
Functions with default arguments
Kotlin can assign default values to function parameters in the function declaration. To invoke the function, you can either omit the arguments with default values or invoke it in the usual way.
Here is a function named printLine with two parameters. The first parameter defines a line, the second parameter is a string that ends this line. Both parameters have default values: an empty line ("") and a new line ("\n").
fun printLine(line: String = "", end: String = "\n") = print("$line$end")
Take a look at this example — we assign values to the parameters using the = operator after the types.
Take a look at a full code snippet. It has the following functions: printLine and main.
fun printLine(line: String = "", end: String = "\n") = print("$line$end")
fun main() {
printLine("Hello, Kotlin", "!!!") // prints "Hello, Kotlin!!!"
printLine("Kotlin") // prints "Kotlin" with an ending newline
printLine() // prints an empty line like println()
}
During the first call, two arguments are passed to the function ("Hello, Kotlin" and "!!!"). During the second call, only one argument is passed ("Kotlin"), the value of the second parameter is default ("\n"). During the third call, we pass no arguments at all, but it still works because both parameters have default values.
The program outputs:
Hello, Kotlin!!!Kotlin
We cannot pass the second argument without the first one. Kotlin cannot understand that we want to assign a value to the second parameter only.
Mixing default and regular arguments
You can also mix default and regular parameters during declaration. For example, the function below finds the max of two integer arguments. It has a special parameter that determines whether it needs to compare numbers by their absolute values. The default value of this parameter is false.
fun findMax(n1: Int, n2: Int, absolute: Boolean = false): Int {
val v1: Int
val v2: Int
if (absolute) {
v1 = Math.abs(n1)
v2 = Math.abs(n2)
} else {
v1 = n1
v2 = n2
}
return if (v1 > v2) n1 else n2
}
fun main() {
println(findMax(11, 15)) // 15
println(findMax(11, 15, true)) // 15
println(findMax(-4, -9)) // -4
println(findMax(-4, -9, true)) // -9
}
In the body of the findMax function, we declare two temporary variables. They store absolute values or the same values of the arguments, depending on the value of absolute. The last line compares temporary variables and returns the value of the corresponding argument.
We call the findMax function in the body of main four times. The first call returns the max value (15), the second call returns the max value of positive numbers (15), the third call returns the max value of two negative numbers (-4), and the last call returns the max by the absolute value of two negative numbers.
Default arguments allow programmers to create optional or special-case behavior for their functions.
Idiom
Default function arguments are a part of the Kotlin idioms. It's a good practice to add default arguments, they are useful when you write complex functions.
fun foo(a: Int = 0, b: String = "") {
}Conclusion
Now you know how to simplify the function call. It can be useful when your function has a lot of arguments, or you need a multi-purpose function and want to set some parameters to a preselected value.