‫Higher order functions are a fundamental concept in functional programming and are also supported in
‫many modern programming languages, including Kotlin.
‫A higher order function is a function that accepts one or more functions as parameters, or it can return
‫a function as its result.
‫Essentially, it treats functions as first class citizen just like any other data type.
‫A higher order function can take one or more functions as arguments.
‫These functions are often referred to as function parameters or function arguments.
‫A higher order function can also return a function as its result.
‫This is sometimes called function composition.
‫Higher order functions are valuable because they enable you to write more reusable and modular code.
‫They allow you to abstract away common patterns and behavior, making your code more concise and easier
‫to maintain.
‫Let's take an example.
‫I'll create a function called operate on numbers to get the parameter a and b a of type int and b of
‫type int and an operation and the operation is a function.
‫I'm passing a function as a parameter, so operate on numbers is a higher order function, and this
‫function, named as operation, looks into the parameter int two integers and the return type of this
‫function is integer.
‫Also, and the return type of this higher order function, it's of type integer.
‫Again.
‫Guys, what I've done here I created a function operate on numbers.
‫Its return type is an integer.
‫It took into the parameters three parameters a and b of type, integer and operation, which is a function.
‫And this function took into the parameter two integers and its return type is integer return operation.
‫And I'm passing a and b.
‫So I'm calling this function and passing a and b that it is passed as parameters.
‫I'll move this function outside the main function.
‫And here I'm going to call this operate on numbers function on different ways.
‫Please I want from you to focus with me because it's very important concept.
‫I'll start with add result.
‫This is a variable I'm calling operate numbers five and three.
‫These are the variables.
‫Also I need to pass the function or the operation here.
‫I'll pass it as a lambda x and y gives x plus y.
‫Here I am passing the operation as this.
‫And when you have the lambda expression as the last parameter, you can pass it like this.
‫Or you can separate it with a comma.
‫Then I'll create another variable called multiply.
‫Result equals to operation on numbers five and three.
‫And here I'm going to pass another lambda expression x and y.
‫And in this case I'm going to multiply x by y.
‫In this way I am calling this function operate on numbers.
‫This higher order function one for addition and one time for multiply result according to the past function.
‫Let me print these results.
‫And let me run the program.
‫And here we go.
‫The additional result is eight and the multiplication result is 15.
‫And this is correct.
‫So in this example we created a higher order function called operate on numbers that takes two integers
‫a and b and a function operation as parameters.
‫The operation function is expected to take two integers and return an integer.
‫In the main function, we call operate on numbers twice, once with an addition function and one with
‫a multiplication function.
‫The higher order function computes the result by calling the provided operation function with the given
‫arguments.
‫The key advantage of higher order functions is that you can swap out the behavior the operation in this
‫case without modifying the operate on numbers function, making it more flexible and promoting code
‫reuse.
‫In summary, higher order functions are an essential feature of functional programming that allows you
‫to work with functions as data, enabling you to write more modular and flexible code by passing and
‫returning functions as values.