Variable scope in codebox?


    Feb 27 2012 | 12:19 am
    I was hoping to be able to copy and paste working codebox objects inside gen~, but when I try I get errors about variables already being declared.
    Is a variable created in a codebox object global to all other codebox objects in the same gen~ patch?
    Is there a way to ensure a variable is treated as local within a single codebox object?

    • Feb 27 2012 | 12:33 am
      you have to turn the code into a function - that way it is not in global codespace but local. maybe then you would not even need to copy paste but simply reuse declared functions?
    • Feb 27 2012 | 1:16 am
      Hey thanks. I think I see a demonstration of what you are talking about in the gen~.interpolation example.
    • Feb 28 2012 | 5:51 am
      It's probably worth mentioning this for anyone reading this thread in the future.
      This code...
      ---
      // a simple integrator:
      my_integrate(in_val) {
      History prev(0);
      prev = prev + in_val;
      return prev;
      }
      out1 = my_integrate(in1);
      out2 = my_integrate(in2);
      ---
      ...creates two separate instances of my_integrate. Each output gets an independent instance. That was not obvious to me from the code, since it looks like two calls to the same function. The way it is written, I would have expected "prev" in a single instance of my_integrate to increase by in1 + in2 for each audio frame (one frame being 1/SampleRate).
      I like the behavior that it actually implements, I just want to point it out in case it is confusing to anyone else.