Exercise 7.1 - Functions and Macros 1 2 3  

Click here to watch this tutorial on YouTube™

Previously, we created blocks of code capable of more flexible and complex musical processes, which can be reused either by way of the clipboard or the .run() mechanism. In this tutorial, instead of placing the code in the pattern directly, we enter it 'outside' the music, in slots designated for useful code that we envisage using in multiple places or situations.
  This represents one of the most powerful concepts in programming: defining functions (or macros) – creating reusable blocks of code to do a specific job. In both programming and music, it is common to repeat elements or processes; functions allow you to write the code once and then use it again and again - but unlike the .run() mechanism, can also adapt to different situations through the use of input arguments.

Explore using code blocks as functions:

Exercise 7.2 - Arguments and Parameters 1 2 3  

Each macro takes one or two input values, known as arguments (x and y, or xx), which can be used to input values to the code and vary its behaviour.
  Like pattern effects seen previously (such as @xx for tempo, SDy for note delay, etc.), the two digits following the effect type / macro number (0 to 9) are parameters that can be specified when the macro is used. There are two ways to use them: you can either use each digit as a separate argument (x and y), each from 0 to F (0 to 15, in hex), or combine both to be a single argument (xy – or, if you prefer, xx), from 00 to FF (0 to 255, in hex). In your macro code, you simply write x, y, and xy (or xx) as values (variables) instead of fixed numbers, and they will assume the value(s) passed from the pattern when the macro is used.

Use arguments to adapt the 0xy effect macro:

Exercise 7.3 - Functions in Practice 1 2 3 

The goal of code is to elegantly represent a process we can use multiple times... in different places, generating different results, based on different inputs. We often want to be able to predict and carefully control what code does, to meet a well-defined purpose, but we can also (especially in music and art) leave variation to chance - e.g. when there are a range of alternatives as good as each other, or when we don't know what we want, and when we're looking for something new.
  Abstracting processes (e.g. into macros or functions) reduces the complexity of systems - in both software and music - making it easier to understand, create, and explore the bigger picture. Instead of editing individual pitches, a function might allow you to explore families of different melodies, harmonies, rhythms, etc.

Explore the remaining functions:

The last macro exploits a unique feature in Manhattan that supports probalistic conditional statements (or "possibly-or-else"). In computing, zero is typically false and all other values (notably including one) are true. In Manhattan, a condition that results in a value between 0.0 and 1.0 becomes a probability of it being true - from 0 (0% chance, always false) to 1 (100%, always true).
  The following examples show how you can easily add stochastic or probalistic processes in your code - i.e. introduce controlled randomness in your music:

0 ? x : y   ' never x, always y
1 ? x : y   ' always x, never y
0.5 ? x : y ' equal chance of x or y
0.1 ? x ' 10% chance of x (or do nothing)
1/4 ? x ' one chance in four (= 0.25 / 25%)
1/3 ? x : (1/2 ? y : z) ' equal chance
0.5 ? x : (0.5 ? y : z) ' x:y:z 50:25:25

Before continuing to the next tutorial, spend a little time coming up with a few of your own functions, in the remaining slots (5xy to 9xy). Feel free to work with the existing content of the pattern, or venture into new channels (muting or unmuting as appropriate).