-
Notifications
You must be signed in to change notification settings - Fork 22
Passing function as parameter
Devrath edited this page Feb 10, 2024
·
4 revisions
Output
Result of Addition:-> 3
Result of Multiplication:-> 1
Code
fun main(args: Array<String>) {
val input = listOf(1,1,1)
val resultAddition = performOperation(input,::addition)
println("Result of Addition:-> $resultAddition")
val resultMultiplication = performOperation(input,::multiplication)
println("Result of Multiplication:-> $resultMultiplication")
}
fun performOperation(input : List<Int>,operation:(List<Int>) -> Int): Int {
return operation(input)
}
// * ****************** Operations ******************
// ADDITION
fun addition(input:List<Int>): Int {
return input.sum()
}
// MULTIPLICATION
fun multiplication(input:List<Int>): Int {
var result = 1;
input.forEach { result *= it }
return result
}
// * ****************** Operations ******************