Kotlin Named Arguments

Making function calls clearer and more flexible

🏷️ What are Named Arguments?

Named arguments let you specify parameter names when calling functions. This makes code more readable, allows parameter reordering, and helps avoid mistakes with multiple parameters.


fun createUser(name: String, age: Int, email: String) {
    println("User: $name, Age: $age, Email: $email")
}

fun main() {
    // Using named arguments
    createUser(name = "Alice", age = 25, email = "[email protected]")
    createUser(email = "[email protected]", name = "Bob", age = 30)
}
                                    

Output:

User: Alice, Age: 25, Email: [email protected]

User: Bob, Age: 30, Email: [email protected]

Named Arguments Benefits

📖

Readability

Makes function calls self-documenting

// Clear what each value represents
connect(host = "localhost", port = 8080)
🔄

Flexibility

Change parameter order freely

// Order doesn't matter
draw(color = "red", x = 10, y = 20)
🛡️

Safety

Prevents parameter mix-ups

// No confusion between width/height
resize(width = 800, height = 600)

Selective

Skip middle parameters easily

// Skip middle parameter
format(text = "Hello", uppercase = true)

🔹 Basic Named Arguments

Use parameter names followed by = when calling functions:

fun formatText(
    text: String,
    fontSize: Int,
    color: String,
    bold: Boolean
) {
    val style = if (bold) "bold" else "normal"
    println("Text: '$text' | Size: ${fontSize}px | Color: $color | Style: $style")
}

fun main() {
    // Traditional positional arguments
    formatText("Hello", 16, "blue", true)
    
    // Named arguments (more readable)
    formatText(
        text = "World",
        fontSize = 20,
        color = "red",
        bold = false
    )
    
    // Mixed approach
    formatText("Kotlin", fontSize = 18, color = "green", bold = true)
}

Output:

Text: 'Hello' | Size: 16px | Color: blue | Style: bold

Text: 'World' | Size: 20px | Color: red | Style: normal

Text: 'Kotlin' | Size: 18px | Color: green | Style: bold

🔹 Named Arguments with Default Values

Named arguments work perfectly with default parameters:

fun configureServer(
    host: String = "localhost",
    port: Int = 8080,
    ssl: Boolean = false,
    timeout: Int = 30,
    maxConnections: Int = 100
) {
    println("Server Config:")
    println("  Host: $host:$port")
    println("  SSL: $ssl")
    println("  Timeout: ${timeout}s")
    println("  Max Connections: $maxConnections")
    println("---")
}

fun main() {
    // Use only defaults
    configureServer()
    
    // Override specific parameters
    configureServer(port = 9000, ssl = true)
    
    // Skip middle parameters
    configureServer(host = "production.com", timeout = 60)
}

Output:

Server Config:

Host: localhost:8080

SSL: false

Timeout: 30s

Max Connections: 100

---

Server Config:

Host: localhost:9000

SSL: true

Timeout: 30s

Max Connections: 100

---

Server Config:

Host: production.com:8080

SSL: false

Timeout: 60s

Max Connections: 100

---

🔹 Mixing Positional and Named Arguments

You can combine positional and named arguments (positional must come first):

fun createButton(
    text: String,
    width: Int,
    height: Int,
    backgroundColor: String = "gray",
    textColor: String = "black",
    enabled: Boolean = true
) {
    val status = if (enabled) "enabled" else "disabled"
    println("Button: '$text' (${width}x${height}) | BG: $backgroundColor | Text: $textColor | $status")
}

fun main() {
    // All positional
    createButton("Click Me", 100, 50)
    
    // Positional + named
    createButton("Submit", 120, 40, backgroundColor = "blue", textColor = "white")
    
    // Mixed with skipping
    createButton("Cancel", 80, 35, textColor = "red", enabled = false)
}

Output:

Button: 'Click Me' (100x50) | BG: gray | Text: black | enabled

Button: 'Submit' (120x40) | BG: blue | Text: white | enabled

Button: 'Cancel' (80x35) | BG: gray | Text: red | disabled

🧠 Test Your Knowledge

What is the correct syntax for named arguments in Kotlin?