Named Parameters in Kotlin
Last Updated :
22 Feb, 2022
A parameter is a value that you can pass to a method. Then the method can use the parameter as though it were a local variable initialized with the value of the variable passed to it by the calling method. Function parameters are defined using Pascal notation - name: type. Parameters are separated using commas, and each parameter must be explicitly typed:
fun powerOf(number: Int, exponent: Int): Int { /*...*/ }
Example
Named parameters in Kotlin allow us to be explicit about naming arguments when passed to a function. This has the benefit that for functions with many parameters, explicit naming makes the intent of each argument clear. This makes the call site more readable. In the following example, we check to see whether the first string contains a substring of the second:
val string = "a kindness of ravens"
string.regionMatches(14, "Red Ravens", 4, 6, true)
To use named parameters, we put the parameter name before the argument value. Here is the function call again, this time with named parameters:
string.regionMatches(thisOffset = 14, other = "Red Ravens",
otherOffset = 4, length = 6, ignoreCase = true)
This second example is more readable at the cost of being more verbose, but it is now clear what each of the parameters is meant for. The final Boolean, which you might have guessed was case sensitivity, is now obvious. If you don't have named parameters, you must check the documentation or source code. Another benefit is that for functions with multiple parameters of the same type, it makes errors less likely as the values can be associated with the name. In the next example, you will see how the function accepts multiple Boolean parameters. And without named parameters, it is easy to swap arguments erroneously:
fun deleteFiles(filePattern: String, recursive: Boolean, ignoreCase:
Boolean, deleteDirectories: Boolean): Unit
Compare the two different styles of calling this function:
deleteFiles("*.jpg", true, true, false)
deleteFiles("*.jpg", recursive = true, ignoreCase = true,deleteDirectories = false)
Did you notice that the first parameter is not named, even when the others are? When calling a function, not all parameters need to be named. The rule is simple: once a parameter has been named, all the following parameters must be named too. Named parameters also allow the parameter order to be changed to suit the caller. For example, the following two examples are equivalent:
val string = "a kindness of ravens"
string.endsWith(suffix = "ravens", ignoreCase = true)
string.endsWith(ignoreCase = true, suffix = "ravens")
Why this is useful will be demonstrated in the next section on default parameters. Changing the order of parameters allows us to selectively choose which default parameters to override.
Conclusion
Named parameters can only be used on Kotlin-defined functions and not on Java-defined functions. This is because the Java code when compiled into bytecode does not always preserve the 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 developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, App code, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
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
TextView in Kotlin Android TextView is simply a view that are used to display the text to the user and optionally allow us to modify or edit it. First of all, open Kotlin project in Android Studio. Following steps are used to create Steps to Implement TextViewSteps by Step implementation for creating an application wh
3 min read
Kotlin Variables In Kotlin, every variable should be declared before it's used. Without declaring a variable, an attempt to use the variable gives a syntax error. The declaration of the variable type also decides the kind of data you are allowed to store in the memory location. In case of local variables, the type o
2 min read
Triple in Kotlin In programming, we often use functions to perform specific tasks. One of the main benefits of functions is that we can call them as many times as we need, and they return a result after performing some computation. For example, an add() function will always return the sum of two numbers we pass to i
3 min read
What is Kotlin Multiplatform? Kotlin Multiplatform or KMP is a technology that allows you to write code once and run it anywhere natively. This native feature motivates developers to prefer KMP over other cross-platform frameworks. KMP supports mobile operating systems such as Android and IOS, web, and desktop operating systems
5 min read
Kotlin | Retrieve Collection Parts The slice function allows you to extract a list of elements from a collection by specifying the indices of the elements you want to retrieve. The resulting list contains only the elements at the specified indices. The subList function allows you to retrieve a sublist of a collection by specifying th
5 min read