Signup/Sign In

Kotlin Lambda Expression

In this tutorial we will discuss about Lambda expressions in Kotlin. Lambda functions or expressions are not new in Kotlin and exists in many other languages like Python, Java and Javascript.

The Lambda function is similar to an anonymous function. An Anonymous function is a function which does not have a name. Basically, Lambda expression is a way to create functions concisely and pass them as argument, return them, etc. We can call them as we call simple functions.

A Lambda expression can be treated as a variable. It means, we can pass it as arguments to functions, return it from functions etc.

Kotlin Lambda Expression

The syntax of lambda expression in Kotlin is:

val nameOfLambda : DataType = { argruments -> bodyOfLambdaFunction }

In lambdas:

  • Arguments are mentioned on the left side of ->. The -> can be eliminated if no argument is present on the left (we will see in Example 1).

  • Body of lambda is present after -> and it cannot be empty.

  • The last expression of lambda is considered as the return statement (see Example 2).

  • If no value is returned or return type is not mentioned, Unit type will be considered as return type, like we explained in Kotlin user-defined functions.

  • Lambda functions are called using the invoke() method or by just adding () after lambda name (with arguments).

This is quite similar to Javascript arrow functions.

Let's take a few code examples.

Kotlin Lambda Expression - Example 1:

Let us start with creating a simple lambda which prints a string:

fun main() {
    // lambda expression with no argument
    val lambda = { println("Lambdas are awesome!")}
    // Calling lambda function using ()
    lambda()
    // Calling lambda function using invoke() function
    lambda.invoke()
}


Lambdas are awesome!
Lambdas are awesome!

In this example, the arguments and the return type were eliminated.

Kotlin Lambda Expression - Example 2:

Let us create another example to find area of a rectangle:

fun main() {
    val area = {length: Int, breadth: Int -> length*breadth} 
/*    
    This lambda is same as:
    fun area(length: Int, breadth: Int){
        return length*breadth
    }
*/
    println("Area of rectangle of dimension 4 and 5 is: ${area(4,5)}")
}


Area of rectangle of dimension 4 and 5 is: 20

Here the return type is automatically inferred as Int.

it in Kotlin Lambda Expression

If there is only one argument present in lambda expression then it can be replaced with the it keyword. It is a shorthand used in Kotlin and very useful. The it keyword will represent the single argument passed to the lambda function.

Let us create an array and print squares of the elements in it using forEach loop:

fun main() {
    val array = arrayOf(10,2,3)
    // Long way
    array.forEach { num -> println(num * num) }
    // Shorthand
    array.forEach { println(it*it) }
}


100
4
9
100
4
9

The it keyword refers to the current element when we use a forEach loop on an array.

Summary

In this tutorial we discussed about Kotlin lambdas or Lambda expressions. Lambdas are useful and make our code concise. Lambdas are also used in Android app development a lot. In the next tutorial we will discuss about Higher order functions.



About the author:
I'm a writer at studytonight.com.