anonymous function kotlin code example
Example 1: kotlin function
fun sum(a: Int, b: Int): Int {
return a + b
}
Example 2: kotlin higher order functions
fun main() {
higherOrderSequence(5, ::square)
}
fun higherOrderSequence(num: Int, myFunc: (Int) -> Int) {
for (x in 1..num) {
print("${myFunc(x)} ")
}
println()
}
fun square(num: Int):Int {
return num * num
}
Example 3: function kotlin
fun happyBirthday(name: String, age: Int): String {
return "Happy ${age}th birthday, $name!"
}
val greeting = happyBirthday("Anne", 32)