In this post, I will try to explain how to write functions in Kotlin by doing a parallel with Java. It’s not rocket science or anything special in this post, but this is the way I understand and remember better how to write in Kotlin, myself (it’s something new to me too :) ), so maybe this post will help others too.
Defining functions
Kotlin
private fun sum(a: Int, b: Int): Int { return a + b }
Java
private int sum(int a, int b) { return a + b; }
Writing function/method declaration analysis
- in both Kotlin and Java, we start writing with the access/visibility modifier “private”. Note! In Kotlin, we do not have to write the visibility modifier “public“. It can be omitted, regarding that is the default visibility when no explicit modifier is provided.
- next, in Kotlin we have to use the “fun” keyword, in order to specify that it is a function. Instead, in Java, we have to write the return type of the method/function.
- next, in both languages we have to write the name of the method/function
- after the name, we have to write parameters between “()” in both languages. The differences appear in writing the parameters. In Java we wrote the type and then the name of the param (int a), whereas in Kotlin we have to write the name first, “:” and then the type (a: Int). Note that the int type in Kotlin starts with capital i (Int). If there are more parameters, they are separated with “,” (in Java and Kotlin).
- next, in Java we would open “{“, but in Kotlin, we have to add “:” and then the return type of the function, and only after that we would open “{” ( (a: Int, b: Int): Int { ). So, the return type in Java is written at the beginning, and in Kotlin at the end of the function/method declaration. NOTE: When there is no return type: in Java we use void, whereas in Kotlin we use Unit (or we can omit writing Unit).
- next, in the body of the method we write the code we need (in this example a sum of a + b). Notice that in Java we MUST add “;” at the end of sum expression (return a + b;), whereas in Kotlin, is not a must. You can add it, but is redundant.
Java | Kotlin |
---|---|
private | private |
int | fun |
sum | sum |
( | ( |
int a, int b | a: Int, b: Int |
) | ) |
{ | : |
return a + b; | Int |
} | { |
return a + b | |
} |
Syntax
Kotlin
visibility fun name(paramName1: type, paramName2: type): returnType {
expresion
}
Java
visibility returnType name(type paramName1, type paramName2) {
expresion;
}
Shorter way of writing the above method in Kotlin
fun sum(a: Int, b: Int) = a + b
This is called a function with an expression body and inferred return type. Inferred type means that the compiler deduced the return type of the expression, so we don’t have to write it explicitly. I like this pretty much as it looks clean and easy to read.
Function returning nothing (in Java, void)
fun printSum(a: Int, b: Int): Unit { println("nothing to return") }
Or
fun printSum(a: Int, b: Int) { println("nothing to return") }
You can learn more at http://kotlinlang.org/docs/reference/basic-syntax.html