Cind language documentation

Parameters of function

A parameters of a function (and also of class methods, accept instructions, sections, object constructor, etc.) are a list of declared or referenced variables.
Syntax of the parameters is a list of variables separated by a comma character inside the brackets ( ... ).
For example:

f = fun(int x,string y) { ... }

is a function which has declared two parameters: x of int type and y of string type. Calling function f with vector of parameters, for example f(2,"a"), will set values from this vector, in given order, to the variables x and y, which will be accessed inside the function code.

It is allowed to call function with a different number of parameters from the numer of parameters in the declaration. Calling function with more parameters will use first of them and omit the rest. Calling function with less parameters sets them at the begining, and the missed values will be set to null or to its default value (see below).

For more about calling functions see: Functions, Vector of values and Function execution.

Parameter type

The type written in the parameter declaration will be a type of the variable of this parameter.
For example:

fun(string a,char b,int c) { ... }

is a function with three parameters with types, respectively, string, char and int.

A value passed to the parameter will be maped to given type and then assigned to the variable, before function execution. The parameter variable can be used as a regular variable inside the function, in particular, its value can be changed.

Parameter can be also declared without type. In that case, any value assigned to the parameter will not be converted and will be just setted.
For example:

fun(x,y) { return x+y; }

is a function, that works for any types of values passed to the parameters.

For more about types of variables and type conversion see Variables and Type conversion.

Default value

When a function is called with fewer parameters than it has in the declaration, then parameters for which there is no value gets the default value. A default value of parameter can be declared after equal character ( = ) in the parameter declaration.
For example:

fun(int x=10,int y,string z="abc") { ... }

is the function declaration, where parameter x has default value 10, parameter y has no default value and parameter z has default value "abc".

When there is no default value declared (and there are too few parameters in the function call), then a parameter will get a null value.

In another example:

f = fun(a=1,b=a+1) { ... }

parameters will be set to the values:

f() --> f(1,2) f(3) --> f(3,4) f(3,5) --> f(3,5) f(3,5,6) --> f(3,5)

The expression of default value is resolved inside function context, just like it was a part of the function code, that is, any function's lock (for example monitor's lock of called method) will be grabbed before resolving the parameters values.

Reference to the variable

it is possible to set a value of parameter of called function directly to the other variable, which is visible from that function in current context, by putting a reference to that variable in parameter declaration. Syntax of the reference to the variable is an ampersand character ( & ) before the variable name.
Example:

var y; ... fun(x,&y,z) { ... }

In above example, every function call will set received second parameter in the y variable.

Parameter preservation

Variable of parameter can be also preserved in the place of its declaration. For example:

fun(x,$y) { ... }

is a function declaration, where parameter variable y will be preserved, starting from the place of declaration and ending at the return from that function.

For more about preservation see Variable preservation.

Getting vector of parameters

It is possible to receive a one single vector of all parameters passed to the function. It can be done by putting dash character ( ^ ) before the parameters.

fun ^(v,s) { ... }

In above example, the v variable will receive vector of all parameters passed to the function and an optional s variable will be set to the number of elements of vector v.

Example of usage:

fun f = fun ^(a) { a.asArray().iterate(fun(x) { host.println("got param: "+x); }); } f(1,5,"Hello",true);

A syntax with a dash character ( ^ ) can be applied as well to the class methods, accept instructions, sections, constructors of objects, etc.
For more about vectors see Vector of values.



Cind programming language 1.0.4

 

Advertisements