Friday, October 28, 2011

function -> func -> fun -> fn. Ok, here we are

It is possible to define functions in your programs.
Arguments are positional.

Example:
fn square(x) {
    return x^2;
}

The last function argument can be a parameter name followed by "..."
In this case the function can take a variable number of arguments which are accesible through a list named as the last parameter.

Example:
fn compare_many(x, others...) {
  for (i : 0; i < len(others); i : i + 1) {
    if (x > [others,i]) {
       output str(x) + " is greater than " + str([others,i]);
    }
    if (x < [others,i]) {
       output str(x) + " is less than " + str([others,i]);
    }
    if (x == [others,i]) {
       output str(x) + " is equal to " + str([others,i]);
    }
  }
}

No comments:

Post a Comment