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?
Also, if I have what you called the lower level subroutine for output 1 in addition to passing the necessary data for output 2, why do I need to write two other functions? Isn't it enough to just write one more to do the computations for output 2 ? On Wednesday, March 4, 2015 at 2:16:11 PM UTC-5, Steven G. Johnson wrote: > > > > On Wednesday, March 4, 2015 at 11:48:28 AM UTC-5, 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: >> > > Refactorize the code: write a lower-level subroutine that does all the > computations for the first output and returns the data needed to compute > the second output. Then write two functions, one which computes and > returns only the first output, and one which computes both the first and > second outputs. > > As others have mentioned, Julia doesn't have nargout. You could use an > optional input argument or a keyword or something to decide whether to > compute the second output, but this is usually a bad idea: making the > number of output arguments depend on the values of the input arguments > makes the function type-unstable, and could screw up performance of > anything that calls your function. >