Kotlin Default and Named Arguments: In this tutorial, you will learn about default and named arguments with the assistance of examples.

Kotlin Default Argument

In Kotlin, you can give default values to parameters in function definition.

On the off chance that a function is called without passing any argument then default arguments are used as parameters of the function definition. Also, when a function is called using argument then the passing argument is used as a parameter in the work definition.

On the off chance that the function is called with arguments passed, those arguments are used as parameters. In any case, if the function is called without passing argument(s), default arguments are used.


How default arguments work?

Case I: All arguments passed


The function foo() takes two arguments. The arguments are given default values. In any case, foo() is called by passing the two arguments in the above program. Subsequently, the default arguments are not used.

The value of letter and number will be ‘x’ and 2 respectively inside the foo() work.

Case II: All arguments are not passed


Here, just one (first) argument is passed to the foo() work. Thus, the primary argument uses the worth passed to the function. In any case, the second argument number will take the default value since the subsequent argument isn’t passed during the function call.

The value of letter and number will be ‘y’ and 15 respectively inside the foo() work.

Case III: No argument is passed


Here, the foo() work is called without passing any argument. Subsequently, the two arguments uses its default values.

The value of letter and number will be ‘a’ and 15 respectively inside the foo() work.


Example: Kotlin Default Argument

fun displayBorder(character: Char = '=', length: Int = 15) {
    for (i in 1..length) {
        print(character)
    }
}

fun main(args: Array<String>) {
    println("Output when no argument is passed:")
    displayBorder()

    println("\n\n'*' is used as a first argument.")
    println("Output when first argument is passed:")
    displayBorder('*')

    println("\n\n'*' is used as a first argument.")
    println("5 is used as a second argument.")
    println("Output when both arguments are passed:")
    displayBorder('*', 5)

}

At the point when you run the program, the output will be:

Output when no argument is passed:
===============

'*' is used as a first argument.
Output when first argument is passed:
***************

'*' is used as a first argument.
5 is used as a second argument.
Output when both arguments are passed:
*****

Kotlin named argument

Prior to discussing named argument, let us think about a little modification of the above code:

fun displayBorder(character: Char = '=', length: Int = 15) {
    for (i in 1..length) {
        print(character)
    }
}

fun main(args: Array<String>) {
    displayBorder(5)
}

Here, we are attempting to pass the second argument to the displayBorder() function, and use the default argument for the first argument. Notwithstanding, this code will give use a blunder. This is on the grounds that the compiler thinks we are attempting to give 5 (Int value) to character (Char type).

To settle the present circumstance, named arguments can be used. Here’ how:


Example: Kotlin named argument

fun displayBorder(character: Char = '=', length: Int = 15) {
    for (i in 1..length) {
        print(character)
    }
}

fun main(args: Array<String>) {
    displayBorder(length = 5)
}

At the point when you run the program, the output will be:

=====

In the above program, we are using a named argument (length = 5) determining that the length parameter in the function definition should take this worth (doesn’t make any difference in the situation of the argument).

The primary argument character uses the default value ‘=’ in the program.


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.


0 Comments

Leave a Reply

Avatar placeholder