![]() |
VOOZH | about |
Kotlin language has superb support for functional programming. Kotlin functions can be stored in variables and data structures, passed as arguments to and returned from other higher-order functions.
In Kotlin, a function that can accept a function as a parameter or return a function is called a Higher-Order function. Instead of Integer, String, or Array as a parameter to a function, we will pass an anonymous function or lambda. Frequently, lambdas are passed as parameters in Kotlin functions for convenience.
Table of Content
We can pass a lambda expression as a parameter to a Higher-Order Function.
There are two types of lambda expressions that can
Output:
GeeksforGeeks: A Computer Science portal for GeeksLet's understand the above program step by step:
At the top, we define a lambda expression that contains print() to print a string to the standard output.
var lambda = {println("GeeksforGeeks: A Computer Science portal for Geeks") } Then, we define a higher-order function that contains one parameter.
lmbd: () -> Unitlmbd is the local name for the receiving lambda parameter.
() represents that the function does not accept any arguments.
Unit represents that the function does not return any value.
In the main function, we have invoked the higher-order function by passing the lambda expression as a parameter.
higherfunc(lambda)
Output:
The sum of two numbers is: 6Let's understand the above program step by step:
At the top, we define a lambda expression defined which returns an Integer value.
var lambda = {a: Int , b: Int -> a + b }Then, we have defined a higher-order function that accepts the lambda expression as a parameter.
lmbd: (Int, Int) -> Int
lmbd is the local name for the receiving lambda parameter.
(Int,Int) represents that the function accepts two integer-type parameters.
-> Int represents that the function returns an integer value.
In the main function, we have invoked the higher-order function by passing the lambda as a parameter.
higherfunc(lambda)We can pass a function as a parameter in a Higher-Order function.
There are two types of functions that can be passed-
Output:
GeeksforGeeks: A Computer Science portal for GeeksAt the top, we define a regular function printMe() which accepts a parameter of String type and returns Unit.
fun printMe(s:String): Unit
(s: String) is the only parameter
Unit represents the return type
Then, we define the Higher-order function as
fun higherfunc( str : String, myfunc: (String) -> Unit)
It receives two parameters, one of String type and another one is a function
str: String represents a string parameter
myfunc: (String) -> Unit represents that it accepts a function as a parameter, which returns Unit.
From the main function, the higher function is invoked by passing the string and the function as arguments.
higherfunc("GeeksforGeeks: A Computer Science portal for Geeks",::printMe)
Output:
The sum of two numbers is: 9At the top, we define the regular function as
fun add(a: Int, b: Int): Int{
var sum = a + b
return sum
}
It accepts two parameters of Integer type, and returns the sum of both integers. Then, we define the higher-order function as
fun higherfunc(addfunc:(Int,Int)-> Int)
It accepts a function that contains two parameters and
calls the regular function addfunc(3,6) by passing the parameters.
From the main function, we invoke the higher-order function by passing the regular function as a parameter
higherfunc(::add)We can return a function from a higher-order function. While returning the function, we have to specify the parameter types and return type of the regular function in the return type of the higher-order function.
Output:
The multiplication of two numbers is: 8In the top of program we define mul() function which accepts two parameters and its return type is also an integer.
fun mul(a: Int, b: Int): IntThen, we define the higher-order function having return type as a function.
fun higherfunc5() : ((Int,Int)-> Int){
return ::mul
}
::mul represents that it return mul() function
(Int,Int) represents that mul accepts two integer-type parameters
Int represents that mul returns an integer value.
In main function, we have called the higher function which returns another function and store this in a variable multiply .
val multiply = higherfunc()Then we invoke the mul() function using the local variable multiply(2,4) by passing two arguments.