Fundamentals of functions in Kotlin

Sachin Kumar
MindOrks
Published in
3 min readOct 19, 2018

Functions in Kotlin are different from the one in Java in the following ways:

  • In Kotlin functions need not to be a part of the class, such as in Java if we want to define the functions we should have the class defined in the .java file. But in Kotlin we can define the function like this:
File name: MyFirstSample.kt
fun main(args: Array<String>){

println("Hello Kotlin !")

}
  • In Kotlin functions can have default parameters like this:

Output: testing2

In the above snapshot, log function has the default parameter “value” which is assigned with value equals to “2”. So, if we call it from main we don’t need to pass the “value” parameter’s value to log function.

But if we don’t set “value” parameter’s value equals to “2”, we will get the compile time error in the main.

So, the benefit of having the default parameter is that we don’t need to pass the value to the calling function and it doesn’t require any change in the code everywhere if we want to change the value just do a change at the single place.

  • In Kotlin functions can have named parameters like this:
fun main(args: Array<String>){

named(count=1,str="Sachin")
named(str="Kumar",count=2)
}
fun named(str: String,count: Int) {

println(str+count)
}

Output:

Sachin1
Kumar2

So, we can easily understand the benefit of using named parameter while calling named function, we can call it by passing the parameters in any order we want.

  • Kotlin can have Extension functions like this:
fun main(args: Array<String>){

println("Sachin Kumar".removeSpace())

var returnedVal = -23.absoluteValue()

println("returned Int val: $returnedVal")
}
// extension function.
fun String.removeSpace() : String{
return this.replace(" ","")
}
// extension function.
fun Int.absoluteValue(): Int{
return -this
}

Output:

SachinKumar
returned Int val: 23

In the above code snippet, “this” is the receiver which is the string value which we are passing as a utility. So, there is no need to create separate classes for both string and integer manipulation above.

Hence, the benefits of using extension function are that it cuts down the use of utility classes like in Java and makes our code easier to read.

  • Kotlin can have infix function like this:
fun main(args: Array<String>) {

var h1 = Header("H1")
var h2 = Header("H2")
// with inflix keyword can use simply fun name.
var h3 = h1 plus h2

println(h3.name)
/* operator overloading, have to use operator keyword with
infix function. */
var h31 = h1 + h2

println(h31.name)

}
class Header(var name:String){

}
//infix function.
infix operator fun Header.plus(other: Header) : Header {

return Header(this.name+other.name)

}

Output:

H1H2
H1H2

So, with infix, we can perform operator overloading in Kotlin and at the same time, we can call the function directly via function name without any need of dot operator.

  • Kotlin also supports tail recursive function if we are using the profound recursive calling like this:
import java.math.BigInteger

fun main(args: Array<String>) {

println(fibonacci(100000, BigInteger("1"),BigInteger("12")))
}

fun fibonacci(length:Int, a: BigInteger, b: BigInteger): BigInteger {
return if (length==0) b else fibonacci(length-1,a+b,a)
}

The above code will lead to Stackoverflow exception. So, we are going to use “tailrec” keyword with fibonacci function:

import java.math.BigInteger

fun main(args: Array<String>) {

println(fibonacci(100000, BigInteger("1"),BigInteger("12")))
}

tailrec fun fibonacci(length:Int, a: BigInteger, b: BigInteger): BigInteger
{
return if (length==0) b else fibonacci(length-1,a+b,a)
}

These were some important fundamentals of the functions in Kotlin which one should be aware of while coding in Kotlin. It has huge benefits in comparison to code in Java, main thing is that these are very easy to use.

I hope you have found it useful, please like this article and share with your fellow coders :).

--

--

Sachin Kumar
MindOrks

Senior Java Backend Dev | Expertise in Java Microservices, Spring Boot Framework & Android apps development.