Product SiteDocumentation Site

11.3.2.5. Function Return Values

All SuperCollider Functions return a value to the interpreter when they have finished execution. As with programs, the value returned is the value of the last statement in the Function. The return value of a Function can be captured, assigned to a variable, and used again later.
This example assigns the result of a Function to a variable.
(
   var mysticalMath =
   {
      arg input = 0;
      input * 23;
   };
   var someNumber = 9;

   someNumber = mysticalMath.value( someNumber );
   someNumber.postln;
   nil;
)
Here is how the program works:
  1. A Function and variable are created, and assigned values.
  2. This line someNumber = mysticalMath.value( someNumber ); executes the mysticalMath function, which multiplies its argument by 23 and returns the value. Then, it assigns the return value of the Function to someNumber. In any statement that contains an assignment, the assignment is always done last. In other words, the Function in this example will always be given an argument of 9, and only after the Function completes execution and returns a value will that value be assigned to someNumber.
  3. The new value of someNumber is displayed.
The program could have been shortened like this:
(
   var mysticalMath =
   {
      arg input = 0;
      input * 23;
   };

   var someNumber = mysticalMath.value( 9 );
   someNumber.postln;
   nil;
)
It could have been shortened even more like this:
(
   var mysticalMath =
   {
      arg input = 0;
      input * 23;
   };

   mysticalMath.value( 9 ).postln;
   nil;
)
Experiment with the shortened versions of the program, ensuring that you know why they work.