title: In Julia, is there a way to pass a variable from a local scope to the enclosing local scope?
body: <p>I am writing some Julia code, wrapped in a function for performance. I need to pass a variable created in a loop to an outer loop but would like to avoid globals for performance reasons:</p>
  
  <pre><code>function f()
      for i=1:1
          for j=1:1
          a=2
          end
      println(a)
      end
  end
  f()
  </code></pre>
  
  <p>This throws an error since the scope of the i-loop does not know about the variable <em>a</em>. Definining <em>a</em> first within the scope in question works:</p>
  
  <pre><code>function f()
      for i=1:1
      a=0
          for j=1:1
          a=2
          end
      println(a)
      end
  end
  f()
  </code></pre>
  
  <p>I am not super happy with that solution though, since it requires to pre-define every variable that I want to pass. Is there no direct way to pass the variable to the enclosing scope?</p>