Cind language documentation
Functions
A function is a selected part of code, that can be executed many times, and each time with different parameters.
Function is also an object, which can be stored in a variable as a value and may be executed from that functional value
by assigning parameters (see Function execution).
Value returned from a function call is the value of the last instruction of code,
or value returned by instruction return during code execution.
Most common function declaration looks like:
fun sum(a,b) {
var c = a+b;
return c;
}
it defines function named sum,
which expects vector of two parameters a and b.
The code of that function is a part inside brackets { ... }.
To execute function, it must be composited with vector of parameters. For example:
x = sum(2,3);
this will execute function sum with parameters (2,3).
During this function call varaiables a=2 and b=3,
code returns value 5,
and finally that returned value will be assigned to the variable x
as a result of the function call.
For syntax of parameters see Parameters of function.
Function type
Function object has a system type fun.
For example:
fun f;
is a declaration of variable f having type fun
(see Variables).
When function is used as a value, it does not have to have a name.
Therefore, the above declaration of function sum may be changed to:
fun sum;
sum = fun(a,b) {
var c = a+b;
return c;
}
where the first word fun is a type of variable sum,
and the second word fun is a part of function definition.
Function, as an object of type fun, accepts no methods, no operators
and cannot be converted to objects of other types.
Arrow notation
The above function sum may be written as:
fun sum = (a,b) > a+b;
which is an arrow notation of the same function.
An operator > defines a function.
The left argument of the operator, a single variable or vector of variables,
is treated as a parameters of the function, and the right statement is a function code.
The parameters of a function belongs to the context of the function and usually has nothing in common
with the context of the place of function declaration.
Examples:
x>x+1 // fun(x) { return x+1; }
(x)>x+1 // the same as above
(x,y)>xy // fun(x,y) { return xy; }
()>{ x.hello(); true } // fun() { x.hello(); return true; }
(x,y)>(y,x) // fun(x,y) { return (y,x); }
(x)>(y)>(z) // fun(x) { return fun(y) { return z; } }
$x >`x/2 // fun($x) { return `x/2; }
Lambda notation
Another kind of function declaration is so called lambda notation  $x.y
(there is no lambda on the keyboard, so we will be using $ character),
where, in addition, parameters will be preserved (see Variable preservation).
The syntax of function in lambda notation is: a dollar character ( $ ),
then a signle variable or a vector of variables, which will be treated as a preserved parameters,
then a dot character ( . ), and finally a statement, which will be a function's code.
Examples:
$x.y // fun($x) { return y; }
$(x).y // the same as above
$(x,y).z // fun($x,$y) { return z; }
$x.$y.z // fun($x) { return fun($y) { return z; }}
And now, the above function sum may be also written as:
fun sum = $(a,b).a+b;
which will work just like a sum function, but with preserved parameters (which is actually not needed here).
Messages as functions
An object with an assigned message to be send to it, is a value of fun type.
Therefore, it can be handled and operated as a function.
Example:
fun g = host.print;
g("Hello"); g(" there!");
In above, g is a variable of function type,
with assigned host.print value (which is a pair of
host object and a print message).
Next, function g is called twice with a different parameters.
For more about messages see Sending messages to objects .
Operators
There are two operators on functions %% and %
which are a function composition (submissions of functions).
As a result of those operators there is a third function, which when called, fistly call the first function,
and then the second one, with parameters received from the first function
(since data returned from a function could be a single value or a vector of values).
h = f %% g; // h is a function composition: f * g
h(x); // calling h(x) will make f(g(x))
The operator (f %% g)(x) is a function composition f(g(x))
and the operator (f % g)(x) is an inverted function composition g(f(x)).
For example, this code ((x,y)>x*y)%%((x,y)>(x+y,xy)) is a function,
which is basically equivalent to a function (x,y)>(x*xy*y),
because (a+b)(ab) = a^{2}  b^{2}.

Advertisements
