Illegal start of simple expression in Scala

scala, syntax

Solution

A variable declaration (`var`) doesn't return a value, so you need to return a value somehow, here's how the code could look like:

object Main {

  def foo(total: Int, coins: List[Int]): Int = {

    if (total % coins.sorted.head != 0)
      0
    else
      recur(total, coins.sorted.reverse, 0)

    def recur(total: Int, coins: List[Int], index: Int): Int = {
      var sum = 0
      sum
    }

  }


}

Problem

I just start learning scala. I got an error "illegal start of simple expression" in eclipse while trying to implement a recursive function: ``` def foo(total: Int, nums: List[Int]): if(total % nums.sorted.head != 0) 0 else recur(total, nums.sorted.reverse, 0) def recur(total: Int, nums: List[Int], index: Int): Int = var sum = 0 // ***** This line complained "illegal start of simple expression" // ... other codes unrelated to the question. A return value is included. ``` Can anyone tell me what I did wrong about defining a variable inside a (recursive) function? I did a search online but can't one explains this error.

Original source