-3
class Demo {

  def add (x:Int, y:Int): Int = {    
    val sum = x+y
    println(sum)   
  }  

}

val vvr = new Demo
vvr.add(1,2)
Maroun
  • 94,125
  • 30
  • 188
  • 241
Mahi
  • 1
  • 2

1 Answers1

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