Default Parameters
In this page:
fun functionName(param: Type, optional: Type = defaultValue): ReturnType {
// body
}
functionName(value)
functionName(value, otherValue)
Declaring a Default Value
Assigning a value directly in the parameter list, like greeting: String = "Hello", makes that parameter optional -- callers who don't pass it get the default.
Example: Declaring a Default Value
// Define the function `greet` taking `name`, `greeting` and returning a String
fun greet(name: String, greeting: String = "Hello"): String {
// Return `"$greeting, $name!"` from this function
return "$greeting, $name!"
}
// Entry point: execution of the program starts here
fun main() {
// Print `greet("Kotlin")` to the console, with a trailing newline
println(greet("Kotlin"))
}
Login to try C/C++/Java/PHP code in the editor
Overriding a Default
A caller can still pass a value for a parameter that has a default, which simply overrides that default for this particular call.
Example: Overriding a Default
// Define the function `greet` taking `name`, `greeting` and returning a String
fun greet(name: String, greeting: String = "Hello"): String {
// Return `"$greeting, $name!"` from this function
return "$greeting, $name!"
}
// Entry point: execution of the program starts here
fun main() {
// Print `greet("Kotlin", "Welcome")` to the console, with a trailing newline
println(greet("Kotlin", "Welcome"))
}
Login to try C/C++/Java/PHP code in the editor
Multiple Default Parameters
A function can have several parameters with defaults, letting callers omit any combination of the trailing ones freely.
Example: Multiple Default Parameters
// Define the function `createUser` taking `name`, `age`, `active` and returning a String
fun createUser(name: String, age: Int = 18, active: Boolean = true): String {
// Return `"$name (age $age, active=$active)"` from this function
return "$name (age $age, active=$active)"
}
// Entry point: execution of the program starts here
fun main() {
// Print `createUser("Alice")` to the console, with a trailing newline
println(createUser("Alice"))
// Print `createUser("Bob", 25)` to the console, with a trailing newline
println(createUser("Bob", 25))
}
Login to try C/C++/Java/PHP code in the editor
Defaults Avoid Overloading
Instead of writing several overloaded functions for each combination of arguments, a single function with defaults covers all the common cases concisely.
Example: Defaults Avoid Overloading
// Define the function `power` taking `base`, `exponent` and returning an Int
fun power(base: Int, exponent: Int = 2): Int {
// Declare a mutable variable `result`, initialized to `1`
var result = 1
repeat(exponent) { result *= base }
// Return `result` from this function
return result
}
// Entry point: execution of the program starts here
fun main() {
// Print "Square: ${power(5)}" to the console, with a trailing newline
println("Square: ${power(5)}")
// Print "Cube: ${power(5, 3)}" to the console, with a trailing newline
println("Cube: ${power(5, 3)}")
}
Login to try C/C++/Java/PHP code in the editor
- Assuming Kotlin needs overloaded functions like Java for optional parameters, instead of using a single function with defaults.
- Putting a non-default parameter after a parameter with a default value without using named arguments, which can force awkward call sites.
- Forgetting that default values are re-evaluated for every call, not computed once and shared across calls.
- A default value is written as
parameter: Type = valuein the function declaration. - Callers may omit any parameter that has a default, and the default value is used instead.
- Default parameters remove the need for multiple overloaded functions in many cases.
- Combining default parameters with named arguments makes it easy to skip some defaults while overriding others.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: