Cookie Preferences

We use cookies to enhance your experience. Choose your preference for cookie usage.

Essential cookies are required for basic functionality. Additional cookies help us improve our service and provide analytics.

View third-party services
  • Google Analytics: Website traffic analysis and conversion tracking
  • RudderStack: Analysis of user interactions with the website
  • Microsoft Clarity: User behavior analysis & session recordings
  • Facebook Pixel: Marketing analysis of user activity
KotlinFunction

Default arguments

Functions with default arguments

Kotlin provides a possibility to assign default values for parameters of a function in its declaration. To invoke such function you can omit arguments with default values as well as to use it in a regular way passing all arguments.

Here is a function named printLine with two parameters. The first parameter is a line to be printed, the second parameter is a string that ends this line. Both parameters have default values: new line and an empty line respectively.

fun printLine(line: String = "\n", end: String = "") = println("$line$end")

Consider this example, we just assign some values to parameters using the = operator after types.

Here is a full code snippet to be executed. It has the function printLine and main.

fun printLine(line: String = "\n", end: String = "") = println("$line$end")

fun main(args: Array<String>) {
    printLine("Hello, Kotlin", "!!!") // prints "Hello, Kotlin!!!"
    printLine("Kotlin") // prints "Kotlin" without an ending
    printLine() // prints an empty line like println()
}

In the first invocation, two arguments are passed to the function ("Hello, Kotlin" and "!!!") just we do before. In the second invocation, only a single argument is passed ("Kotlin"), the value of the second parameter is used as default (""). In the third invocation, no arguments are passed at all, but it still works before both parameters have default values.

The program outputs:

Hello, Kotlin!!!
Kotlin


Note, we cannot pass only the second argument omitting the first one, Kotlin cannot understand that we want to assign a value to the second parameter instead of the first. We will learn what to do in this case in the next topics.

Mixing default and regular arguments

It is also possible to mix default and regular parameters in a function's declaration. The following function finds the max of two given integer arguments. It has a special parameter determines whether it is needed to compare numbers by their absolute values. The default value of this parameter is false.

fun findMax(n1: Int, n2: Int, absolute: Boolean = false): Int {
    val v1: Int
    val v2: Int

    if (absolute) {
        v1 = Math.abs(n1)
        v2 = Math.abs(n2)
    } else {
        v1 = n1
        v2 = n2
    }

    return if (v1 > v2) n1 else n2
}

fun main(args: Array<String>) {
    println(findMax(11, 15)) // 15
    println(findMax(11, 15, true)) // 15
    println(findMax(-4, -9)) // -4
    println(findMax(-4, -9, true)) // -9
}


In the body of findMax function, two temporary variables are declared. They store absolute values or the same values of given arguments depending on the value of absolute. The last line compares the temporary variables and returns the value of the corresponding argument.

There are four invocations of the findMax function in the body of main. The first invocation just returns the max value (15), the second invocation returns the max value of positive numbers (15), the third invocation returns the max value of two negative numbers (-4), and the last invocation returns the max by the absolute value of two negative numbers.

So, default arguments allow programmers to create optional or special-case behaviour for functions.

How did you like the theory?
Report a typo