Default and Named Arguments in Kotlin

1. Introduction

Kotlin simplifies function calls using default and named arguments. These features make functions more flexible, readable, and reduce the need for multiple overloaded versions.


2. Default Arguments

You can assign default values to function parameters. If a value isn’t provided during a function call, the default value is used automatically.


// Example of default arguments
fun greetUser(name: String, age: Int = 18) {
    println("Hello $name, your age is $age")
}

fun main() {
    greetUser("Talha")          // Uses default value: age = 18
    greetUser("Sara", 25)       // Overrides default value
}
    

Output:


Hello Talha, your age is 18
Hello Sara, your age is 25
    

Default arguments reduce the need to overload functions — unlike Java, where multiple versions of a method are required for optional parameters.


3. Named Arguments

Kotlin allows you to specify arguments by their names, making function calls more readable and avoiding confusion when multiple parameters have the same type.


// Using named arguments
fun displayUser(name: String, city: String, country: String) {
    println("$name lives in $city, $country")
}

fun main() {
    displayUser(name = "Ali", city = "Lahore", country = "Pakistan")
    displayUser(country = "USA", city = "New York", name = "John")  // Order can change!
}
    

Output:


Ali lives in Lahore, Pakistan
John lives in New York, USA
    

Named arguments are especially useful when dealing with functions that have many parameters or parameters of the same type.


4. Mixing Positional and Named Arguments

You can combine both positional and named arguments in a single function call — but named arguments must come after positional ones.


fun createAccount(username: String, email: String, isAdmin: Boolean = false) {
    println("User: $username | Email: $email | Admin: $isAdmin")
}

fun main() {
    createAccount("Talha", "talha@example.com")               // Uses default isAdmin = false
    createAccount("Sara", email = "sara@example.com", isAdmin = true) // Mix of positional + named
}
    

5. Default Arguments with Function Overloading

In Java, optional parameters require function overloading. Kotlin eliminates that by supporting default arguments directly — reducing redundant code.


// In Java (Overloading)
void showMessage(String text) { ... }
void showMessage(String text, int count) { ... }

// In Kotlin (Simplified)
fun showMessage(text: String, count: Int = 1) {
    repeat(count) { println(text) }
}

fun main() {
    showMessage("Welcome")      // Default count = 1
    showMessage("Hello", 3)     // Custom count = 3
}
    

6. Interview Questions


7. Example Program


fun calculatePrice(item: String, quantity: Int = 1, pricePerUnit: Double = 10.0) {
    val total = quantity * pricePerUnit
    println("Item: $item | Quantity: $quantity | Total: $$total")
}

fun main() {
    calculatePrice("Notebook")                         // Uses both default values
    calculatePrice("Pencil", quantity = 5)             // Uses one default
    calculatePrice("Bag", quantity = 2, pricePerUnit = 25.5) // Overrides all defaults
}
    

Output:


Item: Notebook | Quantity: 1 | Total: $10.0
Item: Pencil | Quantity: 5 | Total: $50.0
Item: Bag | Quantity: 2 | Total: $51.0
    

Summary: Default and named arguments in Kotlin simplify function design, reduce boilerplate, and make code more expressive — features that are not directly available in Java without using method overloading or builder patterns.