# Higher order functions and lambdas in Kotlin

Kotlin is a programming language that is developed at JetBrains. As many people say about Kotlin that you will not like the Kotlin, you will love it. I have a similar experience with it. Kotlin has many amazing features which will make your life easy as a developer, So today I decided to explain one of the feature which I like, which is Higher order function and lambdas.

# What is Higher order function?

Higher order functions are functions that accept functions as a parameter or return a function or both are Higher Order functions.

# What is Lambda?

Lambda is a function without a name.

Let’s take one simple example before touching the higher order function.

In this example, we will make a simple code of addition which has 3 parts.

- One function named
which accepts 2 Integer parameters and interface of*operation()*.*MathOperation*

`fun operation(x: Int, y: Int, mathOperation: MathOperation) {`

//do some work on x and y if you want

mathOperation.execute(x, y)

}

2. An interface that has one abstract function ** execute()** which accepts 2 Integer parameters.

`interface MathOperation {`

fun execute(firstParam: Int, secondParam: Int)

}

3. And last part is one which will call ** operation()** function which will accept 2 Integer parameters and one

**which implements**

*object***interface.**

*MathOperation*`operation(3, 5, object : MathOperation {`

override fun execute(firstParam: Int, secondParam: Int) {

val value = firstParam + secondParam

print(value)

}

})

Now you can do anything in ** execute()** function which is overridden function, here we are doing addition and printing it.

This is easy right, but it’s not a Kotlin way(Simpler way).

Let’s handle this with the easy way which is using a higher-order function

In higher order function also there are 3 parts.

- The function which accepts 2 params and one higher order function named
*mathOperation**.*

`fun operation(x: Int, y: Int, mathOperation: (Int, Int) -> Int){`

print(mathOperation (x, y))

}

Let’s understand the structure of higher order function ** mathOperation **.

**(Int, Int)**means we are accepting 2 Integer type parameters

**-> Int**means we are returning 1 Integer type parameter.

2. Now move to the 2nd part, which defines what to do with two parameters.

`val add = {x: Int, y: Int -> x + y}`

In an implementation, we are accepting x and y and returning (x + y). This is a lambda.

3. In the last part, we are calling operation with parameters.

`operation(3, 5, add)`

This is it we achieve the same result. If you compare the 1st approach and the 2nd approach, you can easily observe that the 2nd approach is simpler and with less code. Less code means fewer chances of failure.

Let’s consider one more example, what we can pass to ** operation()** function. Here we can also pass subtraction or multiplication as a third param.

val substration = {x: Int, y: Int -> x - y}

val multiplication = {x: Int, y: Int -> x * y}operation(3, 5, subtration)

operation(3, 5, multiplication)

We can create higher functions without parameters and without any return value.

val functionWithoutParam = { print("Inside") }

higherOrderFunction(functionWithoutParam)fun higherOrderFunction(functionWithoutParam: () -> Unit) {

functionWithoutParam.invoke()

}

By using keyword** invoke() **we can invoke a function or we can directly call **functionWithoutParam(). **Kotlin is quite flexible you don’t have to create a separate parameter for lambda and pass to **higherOrderFunction()**, we can do it following way as well.

`higherOrderFunction { print("Inside functionWithoutParam ") }`

Same way in our **add** lambda we can write like below snippet.

`operation(3, 5){x: Int, y: Int -> x + y}`

//or

operation(3, 5){x, y -> x + y}

I would like to highlight one more Kotlin feature, if you have one parameter in lambda then you can use the keyword **` it`** by default and don’t need to mention.

`fun operation(x: Int, square: (Int)-> Int){`

println(square.invoke(x))

}

//call operation function with parameter 4 and lambda outside

operation(4){ it * it }

That's it, folks! In the next blog, we will cover some advanced features of Kotlin. Happy coding!! I hope you guys like this blog. Please let me know in the comments.