Default and Named Arguments in Kotlin

You are here:
Estimated reading time: 3 min

In this tutorial, we will be learning how to use default and named arguments in Kotlin with the help of examples.

Default Arguments in Kotlin

In most programming languages, it is necessary to specify all the arguments that a function accepts while calling that function.

But Kotlin provides us a feature that allows us to bypass this constraint.

We can make a parameter optional while calling the function. i.e. we may decide to pass or not pass an argument while calling a function.

Default arguments in Kotlin do not need to be specified explicitly while calling a function.

Kotlin provides us the facility to set default values of the arguments in the function definition. Therefore, if an argument is not passed in the function call, it’s default value will be used.

Also, if a function having default arguments is called using arguments, then the defined default values are replaced by the passed argument values.

Usage

Example 1: Passing no arguments in the function call

fun main(){
    var result : Int
    result = sumOfAny()
    println(result)
}
fun sumOfAny(a:Int = 4, b:Int = 5, c:Int = 6):Int{
    return a+b+c
}  

In the above example, since no arguments are passed in the function call, the default values for a, b and c are used. Therefore, the output of the above program will be the sum of 4,5 and 6 i.e. 15.

Example 2: Passing some arguments in the function call

fun main(){
    var result : Int
    result = sumOfAny(12,14)
    println(result)
}
fun sumOfAny(a:Int = 4, b:Int = 5, c:Int = 6):Int{
    return a+b+c
}  

In the above example, the values for the function parameters a and b are passed as arguments in the function call and no argument is passed for the function parameter c.

Therefore, the new passed values will be used for the parameters a and b i.e. their default values will now be replaced by 12 and 14. As no argument is passed for the parameter c, it’s default value will be used.

The output of the above program will be the sum of 12, 14 and 6 i.e. 32.

Example 3: Passing all arguments in the function call

fun main(){
    var result : Int
    result = sumOfAny(12,14,16)
    println(result)
}
fun sumOfAny(a:Int = 4, b:Int = 5, c:Int = 6):Int{
    return a+b+c
}  

In the above example, the values for the function parameters a, b and c are passed as arguments in the function call.

Therefore, their default values are replaced by the newly passed values and the output is the sum of 12,14, and 16 i.e. 42.

Example 4

fun productDetails(itemName: String, typeItem: String = "Food"):Int{
    print("$itemName : $typeItem")
}  

For the above example, all these function calls are valid:

productDetails("Yogurt")
productDetails("Harry Potter and the Sorcerer’s Stone","Books")

Note:

These function calls are not valid for the above example:

productDetails()
productDetails(572)

The function call productDetails() is considered invalid because an argument must be passed for the function parameter itemName as no default value is given for that parameter.

The data type of the passed argument and the function parameter must be the same.

The function call productDetails(572) is considered invalid because the argument passed is an integer whereas the function parameter is of type String.

Named Arguments in Kotlin

Arguments that are passed with the name of the parameter they are supposed to be assigned to are known as Named arguments.

Kotlin allows us to set the argument passed to the function to its respective parameter.

Here, the arguments are passed as “key=value” pairs, where the key is the name of the parameter that the argument value is supposed to be assigned to and the value is the argument itself.

This way, it is not necessary to pay attention to the order in which the arguments are passed to a function.

fun main(){
    avengerDetails(role="The Incredible Hulk",mood="Angry",name="Bruce Banner")
}
fun avengerDetails(name:String, role:String, mood:String){
    print("$name is $role.")
    print("\nMood: $mood")
}

Here, the value “The Incredible Hulk” is assigned to the function parameter role, the value “Angry” is assigned to the function parameter mood and the value “Bruce Banner” is assigned to the function parameter name. As you can see, the order in which the arguments are passed is not necessary when using Named Arguments in Kotlin.

Usage

Example 1

fun main(){
    avengerDetails(role="The Incredible Hulk","Angry","Bruce Banner")
}
fun avengerDetails(name:String, role:String, mood:String){
    print("$name is $role.")
    print("\nMood: $mood")
}

The above example will give an Error:

error: mixing named and positioned arguments is not allowed

This is because while using positioned and named arguments together, the positioned arguments must be mentioned before the named arguments. The following function call will not give an error.

fun main(){
    avengerDetails("Bruce Banner",mood="Angry",role="The Incredible Hulk")
}
fun avengerDetails(name:String, role:String, mood:String){
    print("$name is $role.")
    print("\nMood: $mood")
}

Example 2

fun main(){
    avengerDetails(mood="Calm")
}
fun avengerDetails(name:String = "Steve Rogers", role:String = "Captain America", mood:String){
    print("$name is $role.")
    print("\nMood: $mood")
}

Output:

Steve Rogers is Captain America.
Mood: Calm

Example 3

fun main(){
    avengerDetails(name="Anthony Mackie")
}
fun avengerDetails(name:String = "Steve Rogers", role:String = "Captain America", mood:String){
    print("$name is $role.")
    print("\nMood: $mood")
}

Output:

error: no value passed for parameter 'mood'
avengerDetails(name="Anthony Mackie")
                                    ^

Other articles:

Functions in Kotlin

Extensions in Kotlin

Kotlin v/s Java

References:

https://kotlinlang.org/docs/reference/functions.html

Was this article helpful?
Dislike 0
Views: 9