Tutorial : Use Procedures

Procedure is a new concept introduced in HH6. Although similar to sub-patches, procedures offers many advantages.

  • they are calculated on demand, unlike sub-patches which are executed permanently, and allow a good optimization of the CPU.
  • a procedure can be defined in a patch and be called from any other patch anywhere in the workspace, eventually at the same time because they are thread-safe. This makes it possible to implement reusable code and memory optimization.
  • a procedure is by definition iterative and allows you to perform calculations, on the separate elements of an array, like a for loop would do in a traditional programming language.

Example : simple accumulation counter

Let start with a simple example, we want to calculate the sum of nth integers: 1+2+3+...+N = ?. We call this procedure ACCUM.

Add a procedure module in the patch and set its name to ACCUM.

[dbl-click] on the procedure module to edit it and add

and connect them as follow:

The procedure-loop-counter will generate a series of integer 0,1,2,3,.... The data-accumulation will accumulate incoming numbers from the procedure-loop-counter.

As you can notice, there is a small number on top of the wires. They represent the order of inlets processing.

Generally, in Usine, we dont care about the order of inlet processing but in procedures this order is important. In this example we will execute the procedure N times but we have to reset the data-accumulation only at the beginning. So we have to be sure that reset inlet is marked as processing order 1. If its not the case, we must delete/recreate the wire.

We are now ready to use the procedure. For that just add a procedure-call and set its name to ACCUM.

And finally complete the patch as follows:

Few remarks:

  • the procedure is executed (max-number+1) times because the procedure-loop-counter starts by 0 and not 1.
  • each time the max-number changes the procedure is executed, otherwise nothing is calculated.

Example : Fibonacci sequence

In this second example we will create a procedure to generate a Fibonacci value F(N). see https://en.wikipedia.org/wiki/Fibonacci_sequence

F(0)=0
F(1)=1
F(2)=1
...
F(n)=F(n-1)+F(n-2)

It's more complex than the first example because it's an iteration where we have to memorize the two previous values.

Few remarks:

  • the const object has to be initialized at 1;0 value
  • the array-queue-data array-le-max is set to 2 (last two values)
  • when the procedure starts, we have, first, to reset the array-queue-data and then force-enqueue an initialization value (1)
  • on each iteration we enqueue the last sum value

The call of the procedure looks like:

If we want to obtain all the suite (1;2;3;5;8;13;21;34;55) we have to adapt the procedure a little :

Few remarks:

  • we enqueue all the Fibonacci values in a new array-queue-data array-le-max is set to the Fibonacci number
  • we have to invert the array to get the suite ascending

And the call procedure as follow:

Iterations

A procedure can loop on each element of an array.

For example if we want to create an array 1²,2²,3²,4²,....

Of course we could do that with 'normal' math modules but let's try with a procedure, but we remind you that the main advantage of procedures is the fact that they're calculated only on demand.

The procedure is very simple:

But on the array-out we can activate the loop-on-each-element. This option tells Usine that the output module will adapt its size to the loop-count and store each result into a separated element of the array.

the size of the array-out will be set to the loop count.

The procedure call looks like:

We can also loop on each element of an input array. For example if we want to calculate the square of of each element of an array.

Beta Distribution

Another example of procedure utilization in real life: the beta distribution. The following procedure calculate an array of beta distribution coefficients. see https://en.wikipedia.org/wiki/Beta_distribution

The procedure looks like:

The procedure call looks like:

See also

Usine Tutorial

version 6.0.241021

Edit All Pages