August 2018
Beginner
178 pages
4h 56m
English
Functions in Kotlin are declared with the fun keyword. Following the keyword comes the function name, then parentheses, which contain optional function parameters. In Kotlin, return type comes at the end of the function definition, after a colon.
Here's a function in Kotlin that adds two numbers and returns the result:
fun add(a: Int, b: Int): Int { val result: Int = a + b return result}
This function accepts two parameters of type Int (32-bit integer), has a local variable of type Int, and also returns an Int.
If you are familiar with Java, you might have noticed that types of parameters and local variables come after their name. In Java their type declaration comes first.
Calling this function would look like this:
val result: ...
Read now
Unlock full access