Concept Flow - Unit type as void equivalent
Call function
Function runs
Returns Unit
Continue execution
When a Kotlin function returns Unit, it means it returns no meaningful value, similar to void in other languages.
fun greet(name: String): Unit { println("Hello, $name!") } greet("Alice")
| Step | Action | Function Return | Output |
|---|---|---|---|
| 1 | Call greet("Alice") | Unit (implicit) | |
| 2 | Execute println inside greet | Unit (implicit) | Hello, Alice! |
| 3 | greet returns Unit | Unit (implicit) | |
| 4 | Continue after greet call | Unit (implicit) |
| Variable | Start | After greet call | Final |
|---|---|---|---|
| name | undefined | "Alice" | "Alice" |
Kotlin functions returning no value use Unit type. Unit is like void in other languages. Functions return Unit implicitly if no return value. You can declare : Unit explicitly or omit it. Unit has a single value: Unit. Useful for functions that perform actions only.