How To Write A Lambda Function with Kotlin

Aigboje Ohiorenua
5 min readApr 4, 2022

--

In this article, I will be explaining make learn lambdas expression also known as anonymous function in Kotlin. Although they are syntactically similar, Kotlin and Java lambdas have very different features.

Lambdas expression and or Anonymous function ( both mean the same thing) are function literals which means these functions are not declared but passed immediately as an expression.

Example of a kotlin function

this is a basic kotlin function that ask for user name

This is an example of a basic kotlin function, that I named “getInput” this function receives a message as parameter, ask for user input and return the input. Thus is not a lambda function because it has a name. A function without name is called anonymous function. For lambda expression we can say that it is anonymous function.

This is the execution of the function, we ask for a name and saluted the user with “Hello $message” which will print “hello name”.

Now to introduce lambda into this program. Our lambda function will provide and enable us to manipulate what ever the user types. We will call this function that has a lambda as a parameter “manipulateInput”

The white underline is the name of our function.

The brown underline is the data that will be pass into the function, which is a string type.

The blue underline is the parameter that will hold our lambda expression “you can name it any thing you want, I prefer to name mine ‘function’ because it brings understanding to what it does and I will likely not use it as a parameter to hold any Type”.

The green underline is data type your lambda expression will hold and allow you to manipulate. It is not compulsory to pass a parameter in a lambda, but want is the use then of making one if you are not passing a parameter .

The yellow underline is the whole lambda expression that returns nothing.

The pink underline is where we call the lambda expression, and because our lambda expression demand a parameter of type string? So we get the input and pass it to the lambda, so we can do what we like with the it.

This is how you call and execute a lambda expression

I will encourage you to use the the first method because, there will come a time when your lambda expression will have multiple parameters and you will not be able to use the second method.

In our next example we will write a command that will count the number of characters that is been passed to our lambda from the user through the “getInput()” function.

The red underline count the number of characters in the data, message is holding.

The white underline store the number of stores the count that is pass to it from the message.lenght.

The yellow underline displays the message , while the two blue underline format the string with the data you are adding to the string before it is been displayed.

So your output becomes

Now what if you need to return a data from a lambda?. well I have modified the manipulateInput() method to::

In the underline red line our lambda expression returns a String type which we stored in the value count.

While in the underline blue line our manipulateInput() returns an Integer type.

We then execute our manipulateInput() function like this::

Because lambda is anonymous we need to use the parent function which is ‘manipulateInput’ to return data to the parent function. what the blue underline returns is only available for the parent function to use and can not be use for global scope unless the parent returns it to the global scope.

In the white underline, the parent function returns the character count from the string that the lambda expression gave it. Do not forget that in our manipulateInput() function we returned count.length.

The full code below::

fun main() {
//1st method
val count = manipulateInput("Enter you name here: ") { message ->
// this way your user input is store in "message"
// you can do what ever you want with whatever data is in "message"
val count_words = message.length
println("The number of characters in '$message' is '$count_words' ")
return@manipulateInput "$message-Kotlin"
}
//outside the lambda
println()
println(
"The final count of the characters is $count")
}

fun getInput(message: String): String {
print(
message)
return readLine()!!
}

fun manipulateInput(
string: String,
function:(str:String)->String
):Int{
val message = getInput(string)
val count = function(message)
println(
"character length has been modified")
return count.length
}

Lambda is a very powerful weapon in your arsenal as a developer, if you know how to use it, Your function can be so flexible and powerful. Note that local variable cannot be changed in side a lambda expression.

--

--