English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Kotlin default and named parameters

In this article, you will learn about default parameters and named parameters with the help of examples.

Kotlin Default Parameters

In Kotlin, you can provide default values for parameters in the function definition.

If the function is called by passing the parameters, then these parameters are used as arguments. However, if the function is called without passing any parameters, then default parameters are used.

How do default parameters work?

1: All arguments passed

The function foo() has two parameters. The parameters have default values. However, foo() is called by passing two parameters in the above program. Therefore, the default parameters are not used.

In the foo() function, the values of letter and number are 'x' and 2.

2: No all arguments passed, only one argument passed

Here, only one (the first) parameter is passed to the foo() function. Therefore, the first parameter uses the value passed to the function. However, the second parameter number will take the default value because the second parameter was not passed during the function call.

In the foo() function, the values of letter and number are 'y' and 15.

3: No arguments passed

Here, the foo() function is called without passing any arguments. Therefore, both of these parameters use their default values.

In the foo() function, the values of letter and number are 'a' and 15.

Example: Kotlin Default Parameters

fun displayBorder(character: Char = '=', length: Int = 15) {
    for (i in 1..length) {
        print(character)
    }
}
fun main(args: Array<String>) {
    println("Output when no arguments are passed:")
    displayBorder()
    

*' As the first argument.
    println("Output when passing the first argument:")
    displayBorder('*)
    

*' Used as the first argument.
    println("5Used as the second argument.
    println("Output when passing two arguments at the same time:")
    displayBorder('* 5)
}

The output when running the program is:

Output when no arguments are passed:
===================================================================================
*' As the first argument.
Output when passing the first argument:
***************
*' As the first argument.
5As the second argument.
Output when passing two arguments at the same time:
*****

Kotlin Named Arguments

Before discussing named arguments, let's consider some modifications to 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 try to pass the second parameter to the displayBorder() function and use a default parameter for the first parameter. However, this code will result in a usage error. This is because the compiler thinks we are trying to provide a character (Char type) to5(Int value).

To solve this situation, you can use named arguments. The method is as follows:

Example: Kotlin Named Arguments

fun displayBorder(character: Char = '=', length: Int = 15) {
    for (i in 1..length) {
        print(character)
    }
}
fun main(args: Array<String>) {
    displayBorder(length = 5)
}

The output when running the program is:

=====

In the above program, we use named arguments (length = 5)to specify the value to be used in the parameter definition of the length function (irrespective of the position of the actual parameters).

The first parameter character uses the default value in the program '='.