Usually Unit is only used to declare the return value of a function or method:
def output1(in:Int):Unit = {
println(in)
}
output1(2)
def output(fn:(Int) => Unit) = {
fn(2)
}
output(println)
output: 2
In addition, if the compiler judges that the result returned is not of Unit type, it will automatically return () at the end
def output1(in:Int):Unit = {
println(in)
}
val rs = output1(2)
println(rs)
Output result: ()