Function ဆိုတာ code အပိုင်းတစ်ခုကို နာမည်ပေးပြီး ပြန်သုံးနိုင်အောင်လုပ်တဲ့နည်းပါ။ တူညီတဲ့ logic ကိုနေရာအများကြီးမှာ ထပ်ရေးမည့်အစား function တစ်ခုတည်းရေးပြီး ခေါ်သုံးတာက code ကိုသန့်စင်စေပါတယ်။ Kotlin မှာ function ကို 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.အနှစ်ချုပ်
Function သုံးခြင်းက code reuse, readability, testing ကိုပိုကောင်းစေပါတယ်။