title: Scala - Type Mismatch Found Unit : required Array[Int]
body: <p>Why does the method give a compile error in NetBeans</p>
  
  <p>( error in question -- Type Mismatch Found Unit : required Array[Int] )</p>
  
  <pre><code>  def createArray(n:Int):Array[Int] =
    {
        var x = new Array[Int](n)
        for(i &lt;- 0 to x.length-1)
          x(i) = scala.util.Random.nextInt(n)
    }
  </code></pre>
  
  <p>I know that if there was a if clause - and no else clause - then why we get the type mismatch.</p>
  
  <p>However, I am unable to resolve this above error - unless I add this line</p>
  
  <p>return x </p>
  
  <p>The error is not happening because the compiler thinks what happens if n &lt;= 0
  I tried writing the function with n = 10 as hardcoded</p>
  
  <p>Thoughts ?</p>