Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Kotlin: unit (or) void functions

Unit returning functions are similar to void functions in Java. If a Function do not return any value, its return type is Unit.

UnitDemo.kt
fun welcome(name: String) : Unit {
println("Welcome $name")

return Unit // You can simply call return
}

fun main(args: Array) {
var returnValue = welcome("Hari krishna")
println("Returned value is $returnValue")
}


Output
Welcome Hari krishna
Returned value is kotlin.Unit

Specifying the type ‘Unit’  and returning Unit from unit functions is optional.


UnitDemo.kt
fun welcome(name: String) {
println("Welcome $name")
}

fun main(args: Array) {
var returnValue = welcome("Hari krishna")
println("Returned value is $returnValue")
}


Previous                                                 Next                                                 Home


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Kotlin: unit (or) void functions

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×