Introduction
In many languages, calling a function with fewer arguments than parameters requires writing multiple overloaded versions of that function. Kotlin avoids this with default arguments, which let a parameter fall back to a preset value when the caller doesn't supply one. Kotlin also supports named arguments, which let you specify which parameter a value belongs to by name rather than by position, making calls clearer and more flexible.
Cricket analogy: A net bowler always defaults to leg-spin unless the captain names a different variation like googly, so Bumrah doesn't need five separate practice sessions scheduled for every possible delivery type.
Syntax
fun greet(name: String, greeting: String = "Hello") {
println("$greeting, $name!")
}
// Calling with a named argument, out of order
greet(greeting = "Hi", name = "Sam")Explanation
The parameter greeting has a default value of "Hello", so callers can omit it entirely and Kotlin will use the default. When calling greet(greeting = "Hi", name = "Sam"), the arguments are matched by name instead of position, so their order in the call doesn't need to match the order in the function declaration. This is especially useful for functions with many parameters, since it removes the need to create several overloaded versions just to handle different combinations of arguments.
Cricket analogy: When Kohli calls for field=deep, bowler=spin out of order, the captain still matches each instruction to the right fielder by label, just as greet(greeting="Hi", name="Sam") matches by name not position.
Example
fun createUser(name: String, age: Int = 18, isAdmin: Boolean = false) {
println("Name=$name, Age=$age, Admin=$isAdmin")
}
fun main() {
createUser("Alice")
createUser("Bob", 25)
createUser(name = "Carol", isAdmin = true)
}Output
Name=Alice, Age=18, Admin=false
Name=Bob, Age=25, Admin=false
Name=Carol, Age=18, Admin=trueKey Takeaways
- Default arguments are assigned using
=in the parameter list. - Callers can skip parameters that have default values.
- Named arguments let you specify parameters by name, in any order.
- Combining defaults and named arguments reduces the need for function overloads.
- Named arguments improve readability, especially with boolean or numeric parameters.
Practice what you learned
1. What happens if you omit an argument for a parameter that has a default value?
2. Which call correctly uses named arguments out of declaration order for `fun greet(name: String, greeting: String = "Hello")`?
3. What is the main benefit of default and named arguments over traditional overloading?
4. In `fun createUser(name: String, age: Int = 18, isAdmin: Boolean = false)`, what does `createUser("Bob", 25)` produce?
Was this page helpful?
You May Also Like
Functions in Kotlin
Learn how to declare, call, and simplify functions in Kotlin, including single-expression syntax and the Unit type.
Constructors in Kotlin (Primary and Secondary)
Understand how primary and secondary constructors initialize Kotlin objects, including init blocks and constructor delegation.
Lambda Expressions in Kotlin
Understand how to write and use lambda expressions in Kotlin, including trailing lambda syntax and the implicit it parameter.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics