How to Specify Default Values in Kotlin Functions?
Last Updated :
11 Oct, 2022
In Kotlin, you can provide default values to parameters in a function definition. If the function is called with arguments passed, those arguments are used as parameters. However, if the function is called without passing argument(s), default arguments are used. But If you come from the Java world, you might remember that we can't specify a default value to methods. This means that we can't do something like this in Java:
public void foo(int a, int b=10) {
}
We need to write two methods for it, and it is known as method overloading:
public void foo(int al {
}
public void foo(int a, int b) {
}
Also, suppose you have a function with three different kinds of parameters, such as these:
public void foo (int a, double b, String c){
}
Then you'll have seven instances of method overloading:
public void foo (int a, double b, String c),
public void foo (int a, double b) ,
public void foo (double b, String c) ,
public void foo (int a, String c) ,
public void foo (int a) ,
public void foo (double b) ,
public void foo (String c)
Kotlin provides you with default values in the methods by which you can prevent an insane amount of method overloading. Some people might say, "Hey, why don't we use the builder pattern instead of method overloading?". Those people are right, but Kotlin's way is easier than that. Let's see how!
Example
In Kotlin, parameters of functions can have default values, and they are used when the corresponding argument is omitted. This, in turn, reduces the number of overloads. The preceding example with three different types of parameters can be resolved easily in Kotlin with a lot less code. Let's add the mentioned code in the editor, run it, and check the output:
Kotlin
fun main (args: Array<String>){
foo ()
foo (1)
foo (1, 0.1)
foo (1, 0.1, "custom string")
}
fun foo (a: Int=0, b: Double =0.0, c:String="some default value") {
printin ("a=$a ,b=$b ,c = Sc")
}
If you run the preceding code, you will see the following output:
Output:
a-0 , b-0.0 ,c - some default value
a=1 , b=0.0 ,c = some default value
a=1 , b=0.1 ,c = some default value
a=1 , b=0.1 ,c = custom string
As you can see, we didn't have to implement four different methods, and we could map the arguments. The default parameters are used when we don't call the methods by providing explicit parameters, so when you don't pass any parameters, it just uses all the default ones. With the help of named arguments, we can decrease the number of methods even further, but we will cover this in the next recipe. One thing to note is that default arguments will also work with constructors. So you can have a class declaration as follows:
data class Event (var eventName: String? - "", var eventSchedule:
Date? - Date () , var isPrivate: Boolean - false)
Then we can declare objects, as shown:
Event ("Celebration" )
Event ("Celebration", Date () )
Event ("Celebration", Date () , true)
As you can see, with the help of default values in the constructors, we are avoiding the need to implement multiple constructors, which we used to do in Java. Remember that there is a catch here. We won't be able to do this if you are creating objects in Java. This means that doing things as shown in the following code will not be accepted by Java. Now I know you'll be like "What happened to 100% interoperability with Java ?!":
new Event ("Celebration")
new Event ("Celebration", Date () )
new Event ("Celebration", Date () , true)
We just need to do a small modification if we want to expose multiple overloads to Java callers, that is-namely adding @JvmOverloads to the constructors and with default values so that the preceding class declaration becomes this:
data class Event @JvmOverloads constructor (var eventName: String?
= "", var date: Date? - Date (), var isPrivate; Boolean = false)
Also, our method becomes this:
@JvmOverloads fun foo (a: Int=0, b: Double =0.0, c: String="some default value" ) {
printin ("a=$a , b=$b ,c = $c")
This is a small price to pay, but the @JvmOverloads annotation helps our constructors and functions to have default values, called from the Java world too.
If we want our code to work only in the Kotlin world, then we don't need the @JvmOverloads annotation because Kotlin has its own rules by which it can work with default values in constructors and functions. Adding the @JvmOverloads annotation creates all the necessary overloads. So if you decompile your Kotlin bytecode, you will see all the overloaded versions of constructors and functions.
Similar Reads
Passing Variable Arguments to a Function in Kotlin There are a lot of scenarios in which we need to pass variable arguments to a function. In Kotlin, You can pass a variable number of arguments to a function by declaring the function with a vararg parameter. a vararg parameter of type T is internally represented as an array of type T ( Array<T
4 min read
How to Write swap Function in Kotlin using the also Function? also is an extension function to Template class which takes a lambda as a parameter, applies contract on it, executes the lambda function within the scope of calling object, and ultimately returns the same calling object of Template class itself. Swapping two numbers is one of the most common things
3 min read
Transform a List Using Map Function in Kotlin Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
3 min read
How to Pass a Function as a Parameter to Another in Kotlin? Kotlin gives us the power to declare high-order functions. In a high-order function, we can pass and return functions as parameters. This is an extremely useful feature and makes our code much easier to work with. In fact, many of the Kotlin library's functions are high order, such as NBQ. In Kotlin
4 min read
Function Return and Type Hierarchy in Kotlin Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, App code, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
4 min read
How to Type Check an Object in Kotlin? Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
3 min read
Kotlin - Scope Function There are several functions in the Kotlin standard library that help in the execution of a block of code within the context of an object. Calling these functions on an object with lambda expression creates a temporary scope. These functions are called Scope Functions. We can access the object of the
7 min read
Working with Anonymous Function in Kotlin In Kotlin, we can have functions as expressions by creating lambdas. Lambdas are function literals that is, they are not declared as they are expressions and can be passed as parameters. However, we cannot declare return types in lambdas. Although the return type is inferred automatically by the Kot
3 min read
Function Literals with Receiver in Kotlin In Kotlin, functions are first-class citizens. It means that functions can be assigned to the variables, passed as an argument, or returned from another function. While Kotlin is statically typed, to make it possible, functions need to have a type. It exists and it is called function type and these
3 min read
How to Get the Class in Kotlin? Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
3 min read