July 2018
Intermediate to advanced
400 pages
12h 14m
English
Not all functions return a value. Some use side effects instead to do their work, like modifying the state of a variable or calling other functions that yield system output. Think about your player status and aura display code, or the castFireball function, for example. They define no return type and have no return statement. They use println to do their work.
private fun castFireball(numFireballs: Int = 2) =
println("A glass of Fireball springs into existence. (x$numFireballs)")
In Kotlin, such functions are known as Unit functions, meaning their return type is Unit. Click on the castFireball function’s name and press Control-Shift-P (Ctrl-P). IntelliJ will display its return type information (Figure 4.9).
Read now
Unlock full access