Kotlin Higher Order Function Breakdown

Kotlin Higher Order Function Breakdown

Breakdown of Kotlin Higher Order Function.

Rohit Jakhar
·May 11, 2022·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • Introduction:
  • Background
  • Higher Order Function:
  • Conclusion

Introduction:

Hey Android Floks! I am Rohit Jakhar, Native Android Developer having 2+ year experience. I will start new series on Kotlin which Named is Kotlin BreakDown. I will breakdown some Kotlin Topic in easy and fun way. Also i participate in Epic Hashnode Writeathon in #thw-mobile-apps Today we are discuss about Higher Order Function in Kotlin.

Background

If you are Android Developer so you are lover of Kotlin language as I am. Kotlin is modern programming language having a lot of features and adding more features on every release.

If you use Java in Android development so you know importance of Kotlin language and know very well its rich library and function. Higher Order Function is one of the Features out of other Kotlin Features and I swear you used it once in your android journey and know little bit about it. If not so this blog is specially for you.

Higher Order Function:

In Simple Word, Higher order function are those function which accept function as parameter or return function or both.

Pass Function in parameter

We can pass lambda expression as a parameter to Higher Order Function in two ways.

  1. Lambda expression which return Unit
  2. Lambda expression which return any value like Int, String etc.

See Example:

// lambda expression
val hashnode = {
    println("Hashnode: Developer Blog Plateform.") 
}
// higher-order function
fun higherfunc( lmbd: () -> Unit ) {             // accepting lambda as parameter
    lmbd.invoke()                              //invokes lambda expression
}
fun main() {
    higherfunc(hashnode)
}

You see in above example, we pass lambda as parameter in function.

We can pass normal function instead of lambda expression in Higher Order Function.

Return a Function from Another Function:

As in definition of Higher Order Function, we can return a function from another function in Kotlin. Lets see first example.

fun sumOfTwoNum(a: Int, b: Int): Int{
    return a+b
}
//higher-order function declaration
fun higherfunc() : ((Int,Int)-> Int){
    return ::sumOfTwoNum
}
fun main() {
     // invoke function and store the returned function into a variable
    val summation = higherfunc() 
    // invokes the sumOfTwoNum() function by passing arguments
    val result = summation(8,12)  
    println("The summation of two numbers is: $result")
}

As you see, we define a function sumOfTwoNum() function which accept two parameters and its return type is integer. After that we define a higher order function which accept two integer parameter and return type is same as sumOfTwoNum() functions.

and at last we invoke sumOfTwoNum() just passing two int values in higherfunc(8,12) and it return summation in result.

Conclusion

So that is Higher Order Functions.easy-peasy ! Again, Higher Order function are those function who accept function in parameter or return function or both.

If you still have some doubt, ask in comment i will try to solve your doubt happily. If you like my explanation, tell me in comment section so i will make more blog on Kotlin Breakdown.

Did you find this article valuable?

Support Rohit Jakhar by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this