Open In App

Named Parameters in Kotlin

Last Updated : 21 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Kotlin, a parameter is simply a value that we can pass into a function. Inside the function, this parameter acts like a local variable, we can use it just like any other variable. When we define a function, we declare the parameters using Pascal notation (that is, name: type), and if there are multiple parameters, they are separated by commas.

For example:

fun powerOf(number: Int, exponent: Int): Int {
// Function body
}

Here, the function powerOf takes two parameters: number and exponent, both of type Int.

Named Parameters

Kotlin gives us a very useful feature called named parameters. When we call a function, we can specify which value goes to which parameter by using the parameter's name. This is especially helpful when a function has many parameters, as it makes the code clearer and easier to read.

Example: Without Named Parameters

Kotlin
val string = "a kindness of ravens"

string.regionMatches(14, "Red Ravens", 4, 6, true)


So, what do these numbers and the true mean. Unless we check the function definition, it’s hard to tell what each argument is for. It’s not very readable.

Example: With Named Parameters

Kotlin
string.regionMatches(
    thisOffset = 14,
    other = "Red Ravens",
    otherOffset = 4,
    length = 6,
    ignoreCase = true
)


Now the function call is self-explanatory. We can see:

  1. thisOffset = 14 - the starting index in the original string.
  2. other = "Red Ravens" - the string to compare.
  3. ignoreCase = true - case insensitive matching.

Even without seeing the function's definition, we understand exactly what is happening.


Advantages of Named Parameters

1. Improved Readability: Named parameters make the code clearer, especially in functions with many parameters of the same type.

For example:

Kotlin
fun deleteFiles(filePattern: String, recursive: Boolean, ignoreCase: Boolean, deleteDirectories: Boolean)


A call like this is confusing since it's hard to understand what does the boolean values represent.

deleteFiles("*.jpg", true, true, false)

But with named parameters:

deleteFiles(
filePattern = "*.jpg",
recursive = true,
ignoreCase = true,
deleteDirectories = false
)

It's much better. Now it’s impossible to misinterpret what these arguments represent.

2. Fewer Mistakes: When a function has multiple parameters of the same type, it's easy to accidentally swap values. Named parameters reduce this risk because the name clarifies what the value is for.

3. Optional Naming of Parameters: We don’t have to name every parameter. If we start using named arguments, all following arguments must also be named.

deleteFiles("*.jpg", recursive = true, ignoreCase = true, deleteDirectories = false)

Here, only the first argument (filePattern) is passed positionally, and the rest are named.

4. Change the Order of Arguments: Named parameters also allow us to change the order of the arguments if that makes the code clearer.

val string = "a kindness of ravens"
string.endsWith(suffix = "ravens", ignoreCase = true)
string.endsWith(ignoreCase = true, suffix = "ravens")

Both versions are exactly the same, and the compiler accepts either one.


Named Parameters and Default Arguments

Named parameters become even more powerful when used with default arguments (we’ll learn more about these later). Because we can name arguments, we can override only specific parameters while using defaults for others, without worrying about their order.

Note: Named parameters work only with Kotlin functions. We cannot use named arguments with Java functions because Java’s compiled bytecode usually doesn’t keep parameter names.


Next Article
Article Tags :

Similar Reads