On Wednesday, March 4, 2015 at 6:38:28 PM UTC-5, Pooya wrote: > > Thanks for your response. I am not sure what you mean by a lower-level > subroutine. Is that a function inside another one? If yes, How does the > scope of variables work for that? >
>From your description, right now you have: function compute_two_outputs(...) ...do some stuff, get x, y, and z.... ....use x, y, and z to compute output1.... ....use output1, x, y, and z to compute output2.... return output1, output2 end Instead, if you don't always want to compute both outputs, but still want to write the shared computations only once, you can refactor the code to pull out the shared computations into another function (that is "lower level" in the sense that users won't normally call it directly): function some_stuff(...) ...do some stuff, get x, y, and z.... ....use x, y, and z to compute output1.... return output1,x,y,z end function compute_output1(...) return some_stuff(...)[1] end function compute_two_outputs(...) output1,x,y,z = some_stuff(...) ....use output1, x, y, and z to compute output2.... return output1, output2 end