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.

Reply via email to