In this section, we will learn what the default arguments in functions are and how to use them in Kotlin.
Note: we’re assuming you’re already familiar with Kotlin functions.
Kotlin Default Arguments (Optional Parameters)
In Kotlin, functions’ parameters can have default values as well.
A default value is the one we set for a parameter when declaring that parameter for the first time.
We call a parameter with a default argument as optional parameter because in Kotlin we’re allowed to skip over a parameter that has a default value assigned to it already!
This means when you call a function with such type of parameters, you’re allowed to not pass values for those parameters with default values.
Note: for those parameters with default values, if you assign an argument to them, then the default value of the parameter will be replaced with the new argument we’ve passed to it. But if we don’t set any argument for those parameters, then the default value will be used within the body of the function.
Alright, before we get into the details of default parameters, first let’s see the syntax of this type of parameters, then we will continue our discussion from there.
Kotlin Optional Arguments Syntax
fun functionName(parameter:data-type = defaultValue, parameter2: data-type = defaultValue, parameter_n: data-type = defaultValue)
As you can see, the way we set a default value for a parameter is exactly the same as the way we assign a value to a variable. Basically, we use the assignment operator =
to assign a default value to a parameter.
Also note that you can have parameters with default values along with parameters that don’t have a default value.
In that case, put those parameters without default values to come first and after that set the parameters with default values.
fun functionName(parameter1:data-type, parameter2:data-type, parameter_n:data-type = defaultValue)
This way, when calling the target function, if we pass arguments to the function they will be assigned to the parameters without the default arguments first and so we get the chance of only filling these types of parameters and leaving those optional parameters to use their default values if we want.
Example: declaring functions with optional arguments in Kotlin
fun main(){ printDetails("Jack","Bauer") printDetails("Elon","Musk","Tesla & SpaceX") } fun printDetails(firstName:String, lastName:String, company:String = "Apple"):Unit{ println("The name is: ${firstName} and the last name is: ${lastName} and the company is: ${company}") }
Output:
The name is: Jack and the last name is: Bauer and the company is: Apple The name is: Elon and the last name is: Musk and the company is: Tesla & SpaceX
How Does Optional Arguments Work in Kotlin?
Here the printDetails function has three parameters. The first two parameters are set without a default argument, but the last one is set with a default value.
This means when the function is called, we’re required to pass two arguments for the first two parameters, but we can skip over the last parameter. Which in that case, the default value Apple
will be used for the company parameter.
Now when we called this function, the first time we didn’t pass an argument for the company parameter and hence the value Apple used instead.
But the second time we set a value for the company parameter as well. This means now the value Apple is replaced with the one we’ve set here and the new one will be used in the body of the function then.