A function is a way to name a chunk of code so you can reuse it. Instead of rewriting the same logic in multiple places, you write it once as a function and call it — which keeps your code clean. In Kotlin, you declare a function with the fun keyword.
kotlin
fun greet(name: String, day: String = "Sunday"): String {
return "Hello $name, today is $day."
}
fun main() {
println(greet("Aung"))
println(greet("Hla", "Monday"))
}You should see
Hello Aung, today is Sunday. Hello Hla, today is Monday.Summary
Using functions improves code reuse, readability, and testing.