Product SiteDocumentation Site

11.3.3.2. Functional Programming

Functional programming is also easy to understand, but it can be a little bit more difficult to think about complex tasks. Functional programs use Functions to complete all of their work. This is not strictly possible in SuperCollider: it is more imperative than functional, but the creative use of Functions can easily solve some problems that are difficult to write with an imperative approach.
The following example is an extension of the "Imperative" example. Pretend that the following Functions exist, and do the following tasks:
  • getinput : allows the user to enter a number, and returns that number
  • add : adds together the numbers given as arguments, returning the sum
(
   postln( add( getinput, getinput ) );
)
SuperCollider will always execute the inner-most Functions first. This is how the interpreter executes the single-line program above:
  1. Execute the left call of getinput
  2. Execute the right call of getinput
  3. Execute add with the two numbers returned by getinput
  4. Execute postln with the number returned by add
Both imperative and functional programming have advantages and disadvantages. SuperCollider will allow you to use either approach, or a mix of both, when solving problems.