Default And Named Arguments in Kotlin With Example

In this article, we will learn about default and named arguments in kotlin. We will see different examples that demonstrates how to use default arguments in kotlin, named arguments in kotlin etc. At last, you will get some exercises.

There are 2 sections in this article –
(a) Default Arguments in kotlin.
(b) Named Arguments in kotlin.

At first, we will go through functionality of default arguments, how does it work, how to use it etc. After that, we will go through detail explanation of named arguments in kotlin.

Default Arguments in kotlin

In most programming language, we must specify all the arguments that a function accepts while calling that function. In kotlin, we have a feature using which we can get rid of this constraint. We can make parameter optional while calling the function. i.e. we may pass or not pass while a argument while calling function.

Arguments that are not required to specify explicitly while calling a function is called default arguments in kotlin.

Let’s take an example,

fun show(name: String, standard: String = "10th") {
    println("Name = $name , Standard = $standard")
}

Here, show is a function that accepts two arguments (name and standard ). Note that we have initialised the variable standard while defining the function. This is to ensure that if nothing is passed as second arguments while calling this function, it will be assumed that value of variable standard is “10th” i.e. If nothing is passed as second argument while calling function, value of standard would be 10th. So, argument standard is default argument in kotlin.

We may call show() function as below –
show("Rajeev") //First case
show("Rohan") //Second case
show("Rajeev", "11th") //Third case

All are valid calls to the above function. Note that in first and second case, we have not passed value for second argument. So, value of standard will the “10th”. In third case, we have passed “11th” as value of argument standard.

Some invalid ways to call above function are
show() // WRONG
show(10) //WRONG
show('FC') //WRONG

Note that data type of values passed as parameter must match with data type of arguments of function in the same order. Below picture clearly depicts how values, passed to the function while calling any function, are assigned to formal parameters of the function.

Tutorialwing - Default Arguments in kotlin , Kotlin default arguments, kotlin default argument, default argument in kotlin

Tutorialwing – Default Arguments in kotlin

So, Data type of actual parameter must match with data type of formal parameter in same order.

Example to demonstrate default arguments in kotlin –
fun main(args: Array<String>) {

    show()
    show("Rohan")
    show("Rohan", "11th")
}

fun show(name: String = "Rajeev", standard: String = "10th") {
    println("$name , $standard")
}

When you run the program, you will get below output:
Rajeev , 10th
Rohan , 10th
Rohan , 11th

Note that both the formal arguments are default arguments. There are three calls to the show function.
(1) No arguments are passed: – Call without passing any argument is valid because all the arguments of the function are default arguments. So, default value of the arguments will be printed.
(2) Partial arguments are passed: Only one parameter is passed. No problem. Value passed as parameter will be assigned to first formal argument (i.e. name) of the function. Second formal parameter (i.e. standard) will have default value ie. “10th”.
(3) All arguments are passed: Value of the actual parameters will be assigned to the corresponding formal parameters.

1. Guess the output of below program
fun main(args: Array<String>) {

   show()
}

fun show(name: String, standard: String = "10th") {
    println("Name = $name , Standard = $standard")
}
2. Guess the output of below program
fun main(args: Array<String>) {

   show(10)
}

fun show(name: String, standard: String = "10th") {
    println("Name = $name , Standard = $standard")
}
3. Guess the output of below program
fun main(args: Array<String>) {

    show("Male")
}

fun show(name: String = "Rajeev", gender: String, standard: Int = 10) {
    println("Name = $name , gender = $gender, Standard = $standard")
}

What if we want to pass only value for gender? How can we achieve it?

We may solve above problem using named argument in kotlin. Let’s see how we can solve above problem using named argument.




Named arguments in kotlin

Arguments that are passed with name while calling a function are called named arguments in kotlin. For example,

fun main(args: Array<String>) {

    show(gender="Male")
}

fun show(name: String = "Rajeev", gender: String, standard: Int = 10) {
    println("Name = $name , gender = $gender, Standard = $standard")
}

This key-value parameter will act as pre-assigned value for formal parameters of the function show. Here, key is gender in key-value. So, After function call, this key-name (i.e. gender) will be matched with formal parameters of function. When any parameter-name matches, the value of the key will be assigned to that parameter.

Below image clearly depicts the scenario

Tutorialwing - named arguments in kotlin , kotlin named aguments, named argument in kotlin , kotlin named argument, named parameters in kotlin , kotlin named parameters, named parameter in kotlin , kotlin named parameter

Tutorialwing – Kotlin named
parameters

Since we are passing the values of the function parameters using name of the arguments, It is called named argument or named parameter in kotlin.

Did you know?
Since we are passing values as key-value form, Order of values, in which values are passed, does not matter. You can pass values in arbitrary order. So, Below functions calls are same.

show(name = “Rajeev”, gender = “Male”)
show(gender = “Male”, name = “Rajeev”)

Now, we have some exercise for you

1. Guess the output of below program
fun main(args: Array<String>) {

    show(gender = "Male", "Rahul")
}

fun show(name: String = "Rajeev", gender: String, standard: Int = 10) {
    println("Name = $name , gender = $gender, Standard = $standard")
}



2. Guess the output of below program
fun main(args: Array<String>) {

    show(gender = "Male")
}

fun show(name: String = "Rajeev", gender: String, standard: Int = 10) {
    println("Name = $name, gender = $gender, Standard = $standard")
}
3. Guess the output of below program
fun main(args: Array<String>) {

	show(name = “Rahul”)
}

fun show(name: String = "Rajeev", gender: String, standard: Int = 10) {
    println("Name = $name , gender = $gender, Standard = $standard")
}

Conclusion

In this post, we have seen what are default and named arguments in kotlin. We have also gone through different questions that demonstrates use of kotlin default or named arguments.

Leave a Reply