Kotlin Function Variations With Examples

In our previous post, we learned about functions. In Kotlin, we can define a function in different ways. Let's learn about these variations one by one. 

Single Line Function

If there is only one expression, we can shorten the function. Refer to the snippet below - 
fun add(a: Int, b: Int) : Int
    val total = a + b
    return total

// Single Line Function
fun add(a: Int, b: Int) : Int =  a + b

//Kotlin can infer the return type
fun add(a: Int, b: Int) =  a + b

Unit Data Type

When a function does not return any value - the return data type, in this case, is Unit. You can explicitly define a function with a return type as Unit but Kotlin is smart enough to inference.

fun printMessage(msg : String) : Unit
    println("Hi ! " + msg)

// Kotlin can infer the return value in this case

fun printMessage(msg : String)
    println("Hi ! " + msg)
In other programming languages like Java or C#, when the function does not return any value it is indicated using a void keyword. But Kotlin has a specific data type for this scenario.

Function Overloading

You can define multiple functions with the same name but either the parameter count should be different or the type of the parameters need to be different. This concept is known as function overloading.

fun add(a: Int, b: Int) = a + b

// count is different
fun add(a: Int, b: Int, c: Int) = a + b + c 

// type is different
fun add(a: Double, b: Double) = a + b 

Default Value

There are scenarios when you want to pass a default value to the arguments. One way to achieve this is to pass the value every time or you can use the concept of the default values.

fun main()
    printMessage(5) //prints Hello CheezyCode 5 times
    printMessage() //prints Hello CheezyCode once - it uses default value

fun printMessage(count: Int = 1)
    println("Hello CheezyCode")
Explanation - In the above code snippet, we have called printMessage without any value. In this case, count is considered as 1 because we have given a default value 1 to the parameter named count. In simple words, if you pass the value, it will take that value if you don't pass any value it will use its default value. 

Named Arguments

When there are multiple parameters say 10 or more - passing values while calling the method becomes problematic. Developers need to remember the sequence of parameters. The concept of Named Arguments in Kotlin can be used here by passing values with names.

fun main()
    val address = getAddress(street = "123,A Street", city = "Noida")
    // Sequence can be changed
    val address2 = getAddress(city = "Noida", street = "123,A Street")

fun getAddress(street: String, city: String ) : String
    val address = street + " " + city 
    return address
Explanation - In the above code snippet,  we have called the method getAddress by explicitly passing the value of the arguments i.e. names of the argument are used along with the values. This helps in improving the code readability because we know which value is used for which parameter. We can even change the sequence of the values passed.

That's it for this article. For more articles on Kotlin Tutorials For Beginner Series - refer to this.

Happy Learning !! Cheers From CheezyCode.


Popular posts from this blog

Polymorphism in Kotlin With Example

Create Android Apps - Getting Started