kotlin function parameter code example
Example 1: 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 2: kotlin named parameters
fun displayBorder(character: Char = '=', length: Int = 15) {
for (i in 1..length) {
print(character)
}
}
fun main(args: Array<String>) {
println("Output when no argument is passed:")
displayBorder()
println("\n\n'*' is used as a first argument.")
println("Output when first argument is passed:")
displayBorder('*')
println("\n\n'*' is used as a first argument.")
println("5 is used as a second argument.")
println("Output when both arguments are passed:")
displayBorder('*', 5)
}