Kotlin — Lambda Functions

Destan Erik
Ekmob Developer Studio
3 min readFeb 22, 2020
Photo by Jakob Owens on Unsplash

In this article, I’m going to show about how lambda functions are used in Kotlin with samples.

Short Description

These functions are anonymous functions that don’t have a name and they can run any code inside without belonging to any class. A lambda function can be used repeatedly and can take parameters. Moreover these functions can assigned to variable. By using these Lambda functions, more readable codes can be written. Let’s examine the outlines of a lambda function.

Basic Usage

To define a lambda, we need to stick to the syntax:

val lambdaName : Return Type = {argumentList -> codeBody}

Note: The only part of a lambda which isn’t optional is the codeBody.

Below, you can see how a simple lambda function without return type and argument(s) defined and how it is called.

fun main(args: Array<String>) {

val showMessage = { println("Hello there!")}

// invoking function
showMessage()
// the same behaviour
showMessage.invoke()
}

When you run the program, the output will be:

Hello there!
Hello there!

The part surrounded by curly braces is a simple lambda function. And then assigned to variable showMessage and then invoked. We can invoked more than one that function wherever we want. As you can see, that function didn’t take parameters or returned any result. Let’s see how we can pass parameter(s) or return the Lambda result.

Lambda With Parameters and Return Type

When we want to write Lambda Function, there are two optional way. One is them is Type Inference and second them is Type Decleration .

— Type Inference

Kotlin’s type inference allows the type of a lambda to be evaluated by the compiler.

fun main(args: Array<String>) {val result = {number: Int -> number * number}
println("Result: ${result(2)}")
}

When you run the program, the output will be:

Result: 4

In this way, we don’t have to declare return type of Lambda. Kotlin compiler will evaluate the function’s return type.

We can also write the same Lambda Function with more than one parameters:

fun main(args: Array<String>) {val result = {number1: Int, number2: Int -> number1 * number2}
println("Result: ${result(2, 3)}")
}

When you run the program, the output will be:

Result: 6

— Type Decleration

Sometimes we may need to declare the return type of the lambda function.Using the lambda function in this way is called type declaration.

fun main(args: Array<String>) {val result : (Int) -> Int = {number: Int -> number * number}
println("Result: ${result(5)}")
}

When you run the program, the output will be:

Result: 25

If we want to add another parameter to this function, it looks like this:

fun main(args: Array<String>) {val result : (Int , Int) -> Int = {number1: Int, number2: Int -> number1 * number2}
println("Result: ${result(5, 6)}")
}

When you run the program, the output will be:

Result: 30

The pattern is input -> output, however, if the code returns no value we have to use the type Unit . For example:

val noReturn : (Int) -> Unit = { num -> println(num) }

The Lambda Function’s body can have many lines of code. Here is a sample:

fun main(args: Array<String>) {val multiplyAndTurnIntoString = { input : Int ->
val result = input * 5
result.toString()
}
println("Result: ${multiplyAndTurnIntoString(5)}")}

The lambda result type is String because the type of the last command within a lambda block is the returned type.

Conclusion

In this article I simply tried to show the basic principles of Lambda Functions and how Lambda Functions are used. I hope it was useful for you :)

--

--