Why and when to pass variables to sub-functions?

Option (2) makes testing any of the subfunctions in isolation difficult or impossible, so I wouldn’t recommend it. If you’re talking about things like size(a1, 1) as the values you re-compute, then I would definitely recompute them and only pass the array itself. It’s unusual and generally unnecessary to pass an array and the size of that array to a function in Julia. There’s no particular performance benefit to doing so (calling size() should be very nearly free), so all it does is make your function more difficult to correctly use with no perceptible gain.

For quantities which actually take some effort to compute, of course, the answer may be different. But that doesn’t seem to be what you’re talking about here.

8 Likes