← Back to Kotlin Course | Chapter 4: Functions | Lesson 3 of 7

Named Arguments

Named arguments let you say exactly which parameter you're filling in by name, instead of relying only on the order you list them.
Syntax
kotlin
fun functionName(param1: Type, param2: Type = defaultValue) { }

functionName(param1 = value1, param2 = value2)
functionName(param2 = value2, param1 = value1)

Basic Named Arguments

Instead of relying on position, you can specify parameterName = value explicitly when calling a function, which makes the call self-documenting.

Example: Basic Named Arguments

markup
// Define the function `createRectangle` taking `width`, `height` and returning a String
fun createRectangle(width: Int, height: Int): String {
    // Return `"Rectangle ${width}x$height"` from this function
    return "Rectangle ${width}x$height"
}

// Entry point: execution of the program starts here
fun main() {
    // Print `createRectangle(width = 10, height = 5)` to the console, with a trailing newline
    println(createRectangle(width = 10, height = 5))
}

Reordering with Named Arguments

Because named arguments are matched by name rather than position, they can be passed in a different order than they were declared.

Example: Reordering with Named Arguments

markup
// Define the function `createRectangle` taking `width`, `height` and returning a String
fun createRectangle(width: Int, height: Int): String {
    // Return `"Rectangle ${width}x$height"` from this function
    return "Rectangle ${width}x$height"
}

// Entry point: execution of the program starts here
fun main() {
    // Print `createRectangle(height = 5, width = 10)` to the console, with a trailing newline
    println(createRectangle(height = 5, width = 10))
}

Skipping Defaults with Named Arguments

Named arguments let you override just one later default parameter without having to also repeat the earlier defaults you're happy to keep.

Example: Skipping Defaults with Named Arguments

markup
// 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("Carol", active = false)` to the console, with a trailing newline
    println(createUser("Carol", active = false))
}

Improving Readability

For functions with several parameters of the same type, such as multiple booleans, named arguments make the call site far easier to understand at a glance.

Example: Improving Readability

markup
// Define the function `configure` taking `darkMode`, `notifications`, `autoSave`
fun configure(darkMode: Boolean, notifications: Boolean, autoSave: Boolean) {
    // Print "dark=$darkMode, notify=$notifications, autoSave=$autoSave" to the console, with a trailing newline
    println("dark=$darkMode, notify=$notifications, autoSave=$autoSave")
}

// Entry point: execution of the program starts here
fun main() {
    configure(darkMode = true, notifications = false, autoSave = true)
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Mixing named and positional arguments incorrectly; once you use a named argument, arguments after it should generally also be named.
  2. Assuming named arguments change performance or behavior; they only affect readability and call-site flexibility.
  3. Not using named arguments for functions with several Boolean parameters, making call sites hard to read (e.g. f(true, false, true)).
Chapter Summary
  • Named arguments are passed as parameterName = value, independent of declaration order.
  • They greatly improve readability for functions with many parameters or several parameters of the same type.
  • Named arguments can be combined with default parameters to skip earlier defaults while setting a later one.
  • Once a named argument is used, any following arguments in that call should also be named for clarity (and sometimes are required to be).
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.