Are codebox functions always being calculated?

Noah's icon

I apologize if this is a dumb question, but my knowledge of how things work under the hood in traditional programming isn't very deep, but I've been curious about times where codebox might be more efficient computationally as opposed to just organizationally. This also might be a bit of an abstract question, so I hope it makes sense.

When I'm using the codebox, if I declare a function, and it hasn't been called by a conditional, is gen still doing the work to calculate its result?

For example, this code:

X(blah,yadda){

    return atanh(blah*yadda);

    }

Y(foo,bar){

    return cos(bar/foo);

    }

if(in3 > 0){

    out1= Y(in1, in2);

    }

else{

    out2= X(in1, in2);

    }

in this case, if in3 is 0, is gen still calculating the math in the "Y" function, or is it waiting until in3 is greater than 0 to start doing that?

jayrope's icon

To my knowledge gen will calculate that function at audio rate. If a conditional is missing, gen assumes zero. I had a similar question some days ago, got answered here.

Graham Wakefield's icon

In your example, the if() condition test is called at audio rate, but the function X or Y here is not... Y is only called when the if() condition is true, X is only called when the condition is false.

You can verify this by looking at the generated code (send an "exportcode" or a "full_source_code" message to the gen~ object).

Jayrope is correct in the sense that if you didn't assign anything to out1, it would have a default value of zero.