Left Index

Kotlin High 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. For example:

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) { 
	val multiply = {a: Int, b: Int -> a * b}
	sum(10, 20, multiply)
}

fun sum(num1: Int, num2: Int, multiply:(Int, Int) -> Int) {
	var mul = multiply(30, 40);
	
	println("Sum: ${num1 + num2} and multiply: ${mul}")
}
Output:
Sum: 30 and multiply: 1200

In above example function sum() have three arguments first two is of Int type and third one is of function which have two parameter.