Theoretical Paper
- Computer Organization
- Data Structure
- Digital Electronics
- Object Oriented Programming
- Discrete Mathematics
- Graph Theory
- Operating Systems
- Software Engineering
- Computer Graphics
- Database Management System
- Operation Research
- Computer Networking
- Image Processing
- Internet Technologies
- Micro Processor
- E-Commerce & ERP
- Dart Programming
- Flutter Tutorial
- Numerical Methods Tutorials
- Flutter Tutorials
- Kotlin Tutorial
Practical Paper
Industrial Training
Higher order Function
High order function (Higher level function) is a function which accepts function as a parameter or returns a function or can do both. Means, instead of passing Int, String, or other types as a parameter in a function we can pass a function as a parameter in other function.
Let's see the following example:
fun myFun(org: String,portal: String, fn: (String,String) -> String): Unit { val result = fn(org,portal) println(result) } fun myFun(org: String,portal: String, fn: (String,String) -> String): Unit { val result = fn(org,portal) println(result) }
In this above example, we defined a function myFun() with three parameters. The first and second parameter take String and the third parameter as a type of function from String to String. The parameter String to String type means function takes string as an input and returns output as string types.
To call this above function, we can pass function literal or lambda. For example:
fun myFun(org: String,portal: String, fn: (String,String) -> String): Unit { val result = fn(org,portal) println(result) } fun main(args: Array< String>){ val fn:(String,String)->String={org,portal->"$org develop $portal"} myFun("sssit.org","mcatutorials.com",fn) } fun myFun(org: String,portal: String, fn: (String,String) -> String): Unit { val result = fn(org,portal) println(result) } fun main(args: Array< String>){ val fn:(String,String)->String={org,portal->"$org develop $portal"} myFun("sssit.org","mcatutorials.com",fn) }
Output:
sssit.org develop mcatutorials.com
The above higher order function can also be called in another ways as below mention code in main() function:
myFun("sssit.org","mcatutorials.com",{org,portal->"$org develop $portal"})