I tried to create a function without returning a value in Kotlin. And I wrote a function, as in Java, but with Kotlin syntax
fun hello(name: String): Void { println("Hello $name"); }
And I have a mistake
Error: the expression 'return' is required in a function with a block body ('{...}')
After a few changes, I have a working function with zero Void as the return type. But this is not exactly what I need
fun hello(name: String): Void? { println("Hello $name"); return null }
According to the Kotlin Documentation, the Unit Type corresponds to the void type in Java. So the correct function without return value in Kotlin
fun hello(name: String): Unit { println("Hello $name"); }
or
fun hello(name: String) { println("Hello $name"); }
Question: what does Void mean in Kotlin, how to use it and what is the advantage of such use?
java return-type void kotlin
Mara
source share