title: Functions inside a function in Julia
body: <p>In Julia, I have a function for a complicated simulation, <code>monte_carlo_sim()</code> that involves many parameters. Inside this function, I need to call many other functions. I could write stand-alone functions <em>outside</em> of <code>monte_carlo_sim()</code>, but then I'd need to pass many parameters-- many of which are constant inside this function-- which would sacrifice elegance and clarity (and perhaps not take advantage of the fact that these are constant variables?). Is there a performance reason to not include functions within functions? As a toy example here, the temperature <code>T</code> is constant, and if I don't want to pass this variable to my function <code>compute_Boltzmann()</code>, I could do the following. Is there anything wrong with this?</p>
  
  <pre><code>function monte_carlo_sim(temp::Float64, N::Int)
      const T = temp
  
      function compute_Boltzmann(energy::Float64)
           return exp(-energy/T)
      end
  
      # later call this function many times
      for i = 1:1000
          energy = compute_energy()
          b = compute_Boltzmann(energy)
      end
  end
  </code></pre>
  
  <p>Alternatively, I could define a new const type <code>SimulationParameters</code> and pass this to <code>compute_Boltzmann</code> instead, and writing <code>compute_Boltzmann</code> outside of the <code>monte_carlo_sim</code> function as <a href="https://stackoverflow.com/questions/32971681/how-to-pass-parameter-list-to-a-function-in-julia">here</a>? Is this better? I'd be passing more information than I'd need in this case, though.</p>