August 2017
Intermediate to advanced
440 pages
10h
English
So far, most of the functions were defined like procedures (functions that do not return any values). But in fact, there are no procedures in Kotlin and all functions return some value. When it is not specified, the default return value is the Unit instance. We can set it explicitly for demonstration purposes:
fun printSum(a: Int, b: Int): Unit { // 1
val sum = a + b
print(sum)
}
The Unit object is the equivalent of Java's void, but it can be treated as any other object. So we can store it in variable:
val p = printSum(1, 2)
println(p is Unit) // Prints: true
Of course, Kotlin coding conventions claim that when a function returns ...
Read now
Unlock full access