Specifications

5.1 Subpatches 49
objects, build bigger objects from those and still bigger objects in turn. Make
a new object pd squared and when the canvas opens add the parts shown in
Fig. 5 .6.
t f f
*
outlet x-squared
inlet x
fig 5.6: Sub-
patch to com-
pute x
2
To square a number you multiply it by itself. Remember why we
use a trigger to split the input before sending it to each inlet of
the multiply? We must respect evaluation order, so the trigger
here distributes both copies of its input from right to left, the
“cold” r ight inlet o f
*
is filled first, then the “hot” left inlet.
Close this canvas and connect up your new
pd squared
subpatch.
Notice it now has an inlet and outlet on its box. Since we need
two of them duplicate it by selecting then hitting CTRL+D on
the keyboard. Your complete subpatch to ca lculate magnitude should look like
Fig. 5.5. Close this canvas to return to the original topmost level and see
pd magnitude
now defined with two inlets and one outlet. Connect some number
boxes to these as in Fig. 5.4 and test it out.
Abstractions
An abstraction is something that distances an idea from an object, it captures
the essence and generalises it. It makes it useful in other contexts. Superficially
an abstrac tion is a subpatch that exists in a s eparate file, but there is more to
it. Subpatches add modularity and make patches easier to understand, which is
one good reason to use them. However, while a subpatch seems like a separate
object it is still part of a larger thing. Abstractions are re usable components
written in plain Pd, but with two important properties. They can be loaded
many times by many patches and although the same code defines all instances
each instance has a separate internal namespace. They can also take creation
arguments, so you can create multiple instances each with a different behaviour
by typing different creation arguments in the object box. Basically, they behave
like regular programming functions that can be called by many other parts of
the program in different ways.
Scope and $0
Some objects like arrays and send objects must have a unique identifier, other-
wise the interpreter cannot be sure which one we are referring to. In program-
ming we have the idea of scope which is like a frame of reference. If I am talking
to Simon in the same room as Kate I do n’t need to use Kate’s surname every
time I speak. Simon assumes, from context, that the Kate I am referring to is
the mo st immediate one. We say that Kate has local scope. If we create an
array within a patch and call it array1 then that’s fine so long as only one copy
of it exists.