Lambda Functions in Kotlin

Kiran Bablani
3 min readApr 12, 2019

--

Lambda function or expression are the anonymous functions which are not declared but are defined straight way as “expression”. The lambda function is enclosed in “{}”.

Now why we need Lambda functions??

Earlier whenever we used anonymous class having single function in it. We used simpler implementation for it as well but when considering implementation of single function ,it made the code unclear, or weird may be the correct word. So with release of Java 8 and Kotlin , lambda functions were introduced because passing a expression instead of overriding made code clear to read and understand.

Let’s see example for the same

val area = {a :Int -> a*a}

The lambda expression is enclosed in {}. To make it simple to understand, let’s divide the contents inside curly braces into 2 parts : one before the “->” and other after it. So the first part consist of arguments we are passing to function and the other part is what is returned from function. If return type is not provided then the return type ,Kotlin makes the definition computation as return type,for above function it is “Int”.

The above function is expanded as :

fun area(a :Int){
return a*a;
}

A lambda expression parameter declarations go inside curly braces and have optional type defined, the body goes after an -> sign.

Now if we keep the types defined aside : the output will be.

 val area :(Int) -> (Int) = {a -> a*a}

The above way is basically like there is variable area of Function type which takes Int as argument and returns Int ,such as creating a variable with data type.

What is “it”?

it” is implicitly defined name for argument of lambda with single argument. Generally lambda expression has single argument. So the compiler figures out the signature itself and we need not define the param instead we can directly write the function body and param can be accessed as “it”. Example:

fun main(args: Array<String>) { fun getDouble (area : (Int) -> (Int)):Int{
return area(6);
}
println(getDouble({it*2})) //prints 12
}

Generally lambda is passed as last argument into a function . According to convention in Kotlin, when we have to pass lambda to a function as last argument, the lambda argument can be put outside the function parenthesis. Example :

fun main(args: Array<String>) {

fun getArea(x: Int, area: (Int) -> (Int)): Int {
return area(x);
}

val area = getArea(4) { it * it }
println(area) //prints 16
}

Lambda with return value

We can explicitly return value from lambda with “qualified return syntax” or if no return found it considers last expression as return value. Let’s see one example :

fun main(args: Array<String>) {fun getArea (x: Int,area : (Int) -> (Int) ):Int{
return area(x);
}
val area = getArea(4){it*it}
val area_2 = getArea(4){return@getArea it*it}
println(area) //prints 16
println(area_2) //prints 16
}

So, That’s all in Lambda Function.

Also checkout “Lambda function with Higher Order Function

Suggestions are always invited.

--

--