Named Parameters in Kotlin
Last Updated :
21 Jun, 2025
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:
- thisOffset = 14 - the starting index in the original string.
- other = "Red Ravens" - the string to compare.
- 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.
Similar Reads
Pair in Kotlin In programming, we often use functions to perform specific tasks. A great feature of functions is that we can call them as many times as we need, and they return a value after performing some computation. For example, an add() function will always return the sum of two numbers passed to it. However,
3 min read
"lateinit" Variable in Kotlin In Kotlin, there are some tokens that cannot be used as identifiers for naming variables, etc. Such tokens are known as keywords. In simple words, keywords are reserved and have special meaning to the compiler, hence they can't be used as identifiers. For example, asbreakclasscontinuelateinit "latei
3 min read
Overriding Rules in Kotlin In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. You decided your new class has to redefine one of the methods inherited
3 min read
Multiple Return Values in Kotlin Kotlin is a statically typed, general-purpose programming language created by JetBrains, the company that also built world-famous IDEs like IntelliJ IDEA, PhpStorm, and AppCode. Kotlin was first introduced by JetBrains in 2011 and is designed to run on the Java Virtual Machine (JVM). Kotlin is an ob
3 min read
Returns, Jumps and Labels in Kotlin Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and a new language for the JVM. Kotlin is an object-oriented language, and a âbetter lan
3 min read