In Julia, I have a function for a complicated simulation, monte_carlo_sim() that involves many parameters. Inside this function, I need to call many other functions. I could write stand-alone functions outside of monte_carlo_sim(), 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 T is constant, and if I don't want to pass this variable to my function compute_Boltzmann(), I could do the following. Is there anything wrong with this?
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
Alternatively, I could define a new const type SimulationParameters and pass this to compute_Boltzmann instead, and writing compute_Boltzmann outside of the monte_carlo_sim function as here? Is this better? I'd be passing more information than I'd need in this case, though.