Kotlin : Lambdas and Higher-Order Functions

Reading Time: 3 minutes

High order function (Higher level function) is a function which accepts function as a parameter, returns a function and it can do both. We can pass a function instead of passing Int, String, or other data types as a parameter in a function.

Lambdas Expressions

Lambdas Expressions are essentially anonymous functions that we can treat as values , for example -pass lambda expressions as arguments to the function, return them, or do any other thing we could do with a normal object.

Lambda Expressions look like below:

val lambdaName : Type = { argumentList -> codeBody }
val square : (Int) -> Int = { value -> value * value }
val sqrValue = square(12)

There is a simple example:

val returnSameValue : (Int) -> Int = { value -> value }

This is a lambda expression that does nothing. Here the { value -> value } is a complete function in itself. It takes an int as a parameter and returns a value as an int.

In (Int) -> Int

(Int) represents input int as a parameter.

Int represents return type as an int.

So, the returnSameValue is a function that takes a value as int and returns the same value as int.

Another example:

val addTwoNumbers : (Int, Int) -> Int = { a, b -> a + b }

{ a, b -> a + b } is also a lambda expression function that takes two int as the parameters, adds them, and returns an int value.

In (Int, Int) -> Int

(Int, Int) represents two int as the input parameters.

Int represents return type as an int.

So, the addTwoNumbers is a function in itself that takes two int as the parameters, adds them, and returns as an int.

A simple and similar example that relate to the lambda expression given above:-

val addition = addTwoNumbers(15,48)

Now, we have understood the lambda expressions. Let’s move to the Higher-order functions.

Higher-Order Functions

A higher-order function is a function that takes functions as parameters or returns a function.

It’s a function which can take do two things:

  • Can take functions as parameters
  • Can return a function

We will see both the things one by one.

Let’s start with the first one – A function can take a function as parameters.

fun functionAsParameter(xyz: () -> Unit) 
{ 
  // It can take functions 
  // perform a task 
  // execute the function 
  abc() 
}

This takes a function xyz: () -> Unit

xyz is just the name for the parameter. It can be anything.

() -> Unit, this is important.

() represents that the function takes no parameters.

Unit represents that the function does not return anything.

So, the functionAsParameter can take a function that takes zero parameters and does not return anything.

Lets try passing that type of function to the functionAsParameter.

functionAsParameter(
    {
        val student = Student()
        Student.rollNo = 56
        println("Student Roll-Number updated")
    }
)

Here { } is a function in itself. See above highlighted one.

Which creates a student, sets the roll number, and prints an update message. This means it does not take any parameters and does not return anything.

Let’s add fun xyz(), we do not need to write fun xyz() in our code. It is just for understanding purposes now.

functionAsParameter(
    fun xyz(){
        val student = Student()
        Student.rollNo = 56
        println("Student Roll-Number updated")
    }
)

Now, here we can see that we are passing a function xyz() to  functionAsParameter().

As Kotlin provides us a concise way for writing code, it can be changed to the following by removing the ().

A function can return a function

Suppose we have a function addition which takes two integers as parameters and returns the sum of those two parameters as in int.

fun addition(a: Int, b: Int): Int {
  return a + b
}

And, we have a function addFunction which takes zero parameters and returns a function of the type ((Int, Int) -> Int).

fun addFunction(): ((Int, Int) -> Int) {
   // can do something and return function as well
   // returning function
   return ::addition
}

((Int, Int) -> Int)

(Int, Int) means that the function should take two parameters both as the int.

Int means that the function should return value as an int.

Now, we can call the addFunction, get the addition function, and call it like below:

val addition = addFunction()

val result = addition(15,19)

This is what higher-order functions are.

To learn more on Higher order function and lambdas in kotlin click here,
For more tech blogs, please visit Knoldus Blogs.

Written by 

Hi, I am a Software Intern in Knoldus Inc. with experience in technologies like Java Programming, SpringBoot. I am always ready to learn new technologies. My hobbies are watching web series and playing games.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading