kotlin
  1. kotlin-default-and-named-argument

Kotlin Default and Named Argument

Kotlin is a popular programming language that offers many advanced features. In this tutorial, we will discuss the Default and Named Argument features of Kotlin.

Syntax

Default Argument

To use a Default Argument in Kotlin, follow the syntax below:

fun functionName(arg1: Type = defaultValue1, arg2: Type = defaultValue2, ...) {
    // Code block
}

Named Argument

To use a Named Argument in Kotlin, follow the syntax below:

functionName(arg1 = value1, arg2 = value2, ...)

Example

Default Argument

In this example, we have a function called "showMessage" that takes two arguments, "message" and "times", where "times" has a default value of 1.

fun showMessage(message: String, times: Int = 1) {
    for (i in 1..times) {
        println(message)
    }
}

fun main() {
    showMessage("Hello")
    showMessage("Kotlin", 3)
}

Named Argument

In this example, we have a function called "getFullName" that takes three arguments, "firstName", "middleName", and "lastName". We have used Named Arguments to pass the values.

fun getFullName(firstName: String, middleName: String, lastName: String) {
    println("$firstName $middleName $lastName")
}

fun main() {
    getFullName(firstName = "John", middleName = "Doe", lastName = "Smith")
}

Output

Default Argument

When you run the code above, you should see the following output:

Hello
Kotlin
Kotlin
Kotlin

Named Argument

When you run the code above, you should see the following output:

John Doe Smith

Explanation

Default Argument

Default Argument allows you to provide a default value for a function parameter. If a value is not supplied for that parameter, the default value will be used. This can save you time by not having to specify default values every time you call a function.

Named Argument

Named Argument allows you to specify the name of the parameter when you pass an argument to a function. This can make your code more readable and maintainable.

Use

Default Argument and Named Argument can be used to simplify function calls and make code more readable.

Default Argument can be used when you want to provide a default value for a function parameter. This can be useful when you have a parameter that is frequently used with a specific value.

Named Argument can be used when you want to make your code more readable by specifying the name of the parameter when passing an argument to a function.

Important Points

  • In Default Argument, if you provide a value for a parameter, it will override the default value.
  • In Named Argument, you must specify the name of the parameter when passing an argument to a function.
  • Named arguments can be used with default arguments.

Summary

In this tutorial, we discussed the Default and Named Argument features of Kotlin. We covered the syntax, examples, output, explanation, use, important points, and summary of these features. With this knowledge, you can now simplify function calls in your Kotlin code and make it more readable.

Published on: