August 2017
Intermediate to advanced
440 pages
10h
English
Default arguments are mostly known from C++, which is one of the oldest languages supporting them. A default argument provides a value for a parameter in case it is not provided during a method call. Each function parameter can have a default value. It might be any value that matches a specified type, including null. This way we can simply define functions that can be called in multiple ways. This is an example of a function with default values:
fun printValue(value: String, inBracket: Boolean = true, prefix: String = "", suffix: String = "") {
print(prefix)
if (inBracket) {
print("(${value})")
} else {
print(value)
}
println(suffix)
}
We can use this function the same way as a normal function (a function without ...
Read now
Unlock full access