Signup/Sign In

Kotlin Positional, Default and Named Function Arguments

In this tutorial we will discuss about different types of arguments in Kotlin functions. Arguments are the values which are passed to a function when the function is called.

In Kotlin, there are multiple types of arguments that can be used while declaring and calling a Kotlin user-defined function. They are:

  1. Positional arguments

  2. Default arguments

  3. Named arguments

At the end we will also see what are vararg.

Kotlin Positional Arguments

These are the basic type of arguments present in any language. These are position based arguments. The first argument corresponds to the first parameter in function definition, second argument to second parameter and so on.

For example,

fun main() {
    printInfo("Groot", 2)
}
fun printInfo(name: String, age: Int) {
    println("Hi! My name is $name and I'm $age months old.")
}

In the code above, the "Groot" and 2 are positional parameter for name and age function parameters. If we try reversing their position i.e. 2 first and "Groot" later, it will not work with positional arguments. We can achieve this using named arguments though.

Kotlin Default Arguments

Default arguments are used to provide default value to a function parameter. Consider a situation in which we want to assign a default value to function parameter if any value is not passed by the user while calling the function, then the default value will be used. We can achieve this using default arguments.

We can define the default value in the function definition:

fun printInfo(name: String = "XYZ", age: Int = 18) {
    println("Hi! My name is $name and I'm $age months old.")
}

Here, we defined default value XYZ for name and 18 for age. Let us call this function using different number of arguments:

fun main() {
    // Providing both the arguments
    printInfo("Groot", 2)

    // Providing none of the arguments
    printInfo()

    // Providing only name
    printInfo("Groot")
}

// function definition
fun printInfo(name: String = "XYZ", age: Int = 18) {
    println("Hi! My name is $name and I'm $age months old.")
}


Hi! My name is Groot and I'm 2 months old.
Hi! My name is XYZ and I'm 18 months old.
Hi! My name is Groot and I'm 18 months old.

Explanation:

  • In the first case, both the arguments are provided and these arguments are chosen over default arguments.

  • In second case, none of the argument is provided. So, default arguments are used.

  • In third case, argument for name is provided but for age default argument is taken.

But in this case, if we try to pass only age we will get an error.

// Providing only age
printInfo(10)          // ERROR

It is because the complier will try to map the first argument i.e. 10 with the first parameter name. To achieve this kind of functionality, we will use Named argument.

Kotlin Named Argument

While calling the function, we can specify that a particular argument is meant for a particular parameter using the name of the parameter. These types of arguments are known as Named arguments.

Let us call the same prinfInfo() method by passing only age. This time we will call it with named argument:

fun main() {
    // Providing only age
    printInfo(age = 10)         
}
fun printInfo(name: String = "XYZ", age: Int = 18) {
    println("Hi! My name is $name and I'm $age months old.")
}


Hi! My name is XYZ and I'm 10 months old.

Using named argument, we can also rearrange the arguments. We just need to give the name of the parameter and use name = value format while calling the function.

Do remember one thing, if we start using named arguments in a function, we cannot use positional arguments for rest of the parameters. For example:

fun main() {
    printInfo("XYZ", gender = "Male", "India")          // ERROR

}
fun printInfo(name: String, gender: String, Nationality: String) {
    println("Hi! My name is $name")
    println("My gender is $gender")
    println("I'm from $name")
}

After using named argument gender, we need to give name for each argument.

Kotlin vararg

The vararg stands for variable number of arguments. Kotlin allows us to pass variable number of arguments to a function using vararg.

Let us create a function which can take variable number as arguments and returns their sum:

fun main() {
    findSum(1,2,10,6,5)
    findSum(1)
}
// function taking variable number of arguments
fun findSum(vararg numbers: Int): Int {
    var sum = 0
    // loop around the arguments
    for (number in numbers)
        sum += number
    return sum
}

Summary

In this tutorial we discussed about various types of arguments and their usage. We also discussed about varargs in Kotlin. In the next tutorial we will discuss about Lambda functions/expressions in Kotlin.



About the author:
I'm a writer at studytonight.com.