Functions

When you find yourself doing the same task several times, it may be a good time to create a reusable function. A function, as you learned in the Enclosures section, is something enclosed within curly braces. David Touretzky introduces the idea of function in this way: “think of a function as a box through which data flows. The function operates on the data in some way, and the result is what flows out.”1

f = { 2 + 2 }; // define the function
f.value; // put the function to work

The function above is not terribly useful, as it only knows how to do one thing (add 2 and 2). Normally you will want to define functions that can give you different results depending depending on varying input arguments.

f = {arg a, b; ["a plus b", a+b, "a times b", a*b].postln}; // define function
f.value(3, 7); // now you can give any two numbers as arguments to the function
f.value(10, 14);

// Compare:
~sillyRand = rrand(0, 10); // not a function
~sillyRand.value; // evaluate several times
~sillyRand2 = {rrand(0, 10)}; // a function
~sillyRand2.value; // evaluate several times

As a last example, here’s one very useful function.

// Use this function to decide how to spend your Summer days
(
~whatToDo = { 
var today, dayName, actions;
    today = Date.getDate.dayOfWeek;
    dayName = 
    case
    {today==0} {"Sunday"}
    {today==1} {"Monday"}
    {today==2} {"Tuesday"}
    {today==3} {"Wednesday"}
    {today==4} {"Thursday"}
    {today==5} {"Friday"}
    {today==6} {"Saturday"};
    actions = ["boomerang throwing", "arm wrestling", "stair climbing", "rope climbing", "underwater hockey", "pea shooting", "a nap marathon"];
    "Ah, " ++ dayName ++ "...! " ++ "What a good day for " ++ actions.choose;
};
)

// Run it in the morning
~whatToDo.value;

TIP: Another common notation to declare arguments at the beginning of a Function is: f = {|a, b| a + b}. This is equivalent to f = {arg a, b; a + b}


  1. Touretzky, David. COMMON LISP: A Gentle Introduction to Symbolic Computation. The Benjamin/Cummings Publishing Company, Inc, 1990, p. 1. That’s the book that inspired the title of this tutorial.