kotlin function
fun sum(a: Int, b: Int): Int { return a + b }
function kotlin
// Declare a function in Kotlin fun happyBirthday(name: String, age: Int): String { return "Happy ${age}th birthday, $name!" } // Call function val greeting = happyBirthday("Anne", 32)
Source: kotlinlang.org
Kotlin Simple Function Program
fun callMe() { println("Printing from callMe() function.") println("This is cool (still printing from inside).") } fun main(args: Array<String>) { callMe() println("Printing outside from callMe() function.") }
kotlin function
inline fun repeat(times: Int, action: (Int) -> Unit) Executes the given function action specified number of times. A zero-based index of current iteration is passed as a parameter to action. // greets three times repeat(3) { println("Hello") }
how to write a function in kotlin
// Use 'fun' keyword to declare function // 'num' is the parameter and is of type Int // The ':Int' indicates the return type fun square(num: Int):Int { return num * num }
make function kotlin
fun main() { println("This is a function") }
Kotlin Function
fun main(args: Array<String>) { var number = 5.5 print("Result = ${Math.sqrt(number)}") }
Kotlin How to call a function?
callme()
Kotlin Function Example
fun main() { println("Hello, world!!!") x() } fun x(){ println("HELLO WORLD") }
function in kotlin
kotlin function