Thanks. I thought of using a boolean variable as an input indicating if I need the second output or not, which is exactly like what you mentioned, but was wondering if this is idiomatic or not!
On Wednesday, March 4, 2015 at 11:59:57 AM UTC-5, Avik Sengupta wrote: > > Yes, Julia doesnt have "nargout". The idiomatic way to do this would be to > use optional arguments: > > http://julia.readthedocs.org/en/latest/manual/functions/#optional-arguments > > So: > > function f(a, b=0) > if b > 0 > #do the computations for second argument > end > #rest of computation > end > > Use a default value that makes sense in your domain. > > On Wednesday, 4 March 2015 16:48:28 UTC, Pooya wrote: >> >> I have a function with two outputs. The second one is computationally >> expensive, so I want to avoid the computation unless the user needs it. I >> read on another post in the group that the solution in this case is usually >> to define two functions, but in this case I basically need to do all the >> computations for the first output to compute the second. Is there any nicer >> way to do this in julia? In MATLAB, I would just say: >> >> If nargout > 1 >> # do all the computations for second output >> end >> >
