title: Scala return statements in anonymous functions
body: <p>Why does an explicit return statement (one that uses the <code>return</code> keyword) in an anonymous function return from the enclosing named function, and not just from the anonymous function itself?</p>
  
  <p>E.g. the following program results in a type error:</p>
  
  <pre><code>def foo: String = {
    ((x: Integer) =&gt; return x)
    "foo"
  }
  </code></pre>
  
  <p>I know it's recommended to avoid the <code>return</code> keyword, but I'm interested in why the explicit and implicit return statements have a different semantics in anonymous functions.</p>
  
  <p>In the following example, the return statement "survives" after <code>m</code> has finished executing, and the program results in a run-time exception. If anonymous functions didn't return from the enclosing function, it would not be possible to compile that code.</p>
  
  <pre><code>def main(args: Array[String]) {
    m(3)
  }
  
  def m: (Integer =&gt; Unit) =
    (x: Integer) =&gt; return (y: Integer) =&gt; 2
  </code></pre>