class Demo {
def add (x:Int, y:Int): Int = {
val sum = x+y
println(sum)
}
}
val vvr = new Demo
vvr.add(1,2)
Asked
Active
Viewed 586 times
1 Answers
1
You defined the add
function to return Int
, but you're not returning anything.
Change to the following:
def add (x:Int, y:Int): Int = {
val sum = x+y
println(sum)
sum
}
Now you can assign the result to a variable.
Alternatively, you can change Int
to Unit
in the function's definition.

Maroun
- 94,125
- 30
- 188
- 241