CalcIt Commands

FUNCTION
...
...
END;

This command is used to create local functions inside a code to better control its complexity. E.g.

FUNCTION WithTAX(Amount,Tax);
 Inc(Amount,Tax);
END;

Local functions can access all the variables already defined globally in your code. Also can be recursive.

Variables that appear for first time in a local function are created only in the scope of this function (local variables). These variables cannot be accessed outside the local function that create them. Local variables can be of the following types

  • Simple
  • Array
  • Automation
  • Buffer
  • Form
  • Class
  • File

The parameters can be of the following types:

  • Simple (IN or IN/OUT)
  • Array (IN or IN/OUT)
  • Automation (IN or IN/OUT)
  • Buffer (IN or IN/OUT)
  • Function parameter (explained later) (IN)
  • File (IN/OUT)
  • Form (IN/OUT)
  • Class (IN/OUT)

We can define default values for simple parameters.

E.g. Declaring 

FUNCTION WithTAX(Amount,Tax=18);
 Inc(Amount,Tax);
END;

We can call it in code as

Total:=WithTAX(1000);

The above omits the second parameter and so its default value is assumed (18).

Parameters with default values in the middle of the parameter list can be omitted simply putting only their comma. E.g.

AnyFuncName(10,,12,,16); //two parameters omitted

Additionally default values can be defined for array IN parameters too. E.g.

AnyFuncName(a,arr b=[10,20,30]);

Using symbol & we can declare IN/OUT parameters. e.g.

FUNCTION TestByRef(&a);
 a:=a+5;
END;

b:=10;
TestByRef(b);

In the above example, after the execution of TestByRef function, variable b will have the value of 15. Parameter a is declared as IN/OUT parameter. Using IN/OUT parameters we can pass a value to a function (IN) but also we can get a value from it (OUT).

To declare array parameters (IN or IN/OUT) we precede the name of the parameter with keyword arr. E.g.

Array IN parameter:

Function DisplayArr(arr a);
 for(i,1,size(a));
  print(a(i));
 end;
end;

Array IN/OUT parameter:

Function AlterArr(arr &a);
 for(i,1,size(a));
    //change a. The passed array is affected
  a(i):='(' & a(i) & ')';
 end;
end;

Additionally SET command can be used to create array variables locally:

Function LocalArr;
 set a(100);
 
//do something with a
 for(i,1,size(a));
  a(i):=i*100;
 end;
end;

If array a was not created globally before the above function then it is created locally in LocalArr and cannot be accessed outside its scope.

The way to declare automation object parameters is the same as for arrays but using obj instead of arr.

Automation object IN parameter:

Function HandleObj(obj o);
 
//do something
end;

Automation object IN/OUT parameter:

Function ChangeObj(obj &a);
 
//do something
end;

Additionally SET command can be used to create automation variables locally:

Function LocalAutoObjects;
 set o=OBJECT('Excel.Application');
 
//do something with o
 for(i,1,10);
  o.Cells(i,10):=i*100;
 end;
end;

If object o was not created globally before the above function then it is created locally in LocalAutoObjects and cannot be accessed outside its scope.

Function parameters can be declared. Function parameters, as their name imply, accept in their place the name of another local function with the specific parameters interface defined in the declaration of the parameter. For example

function MySort(arr& a, Value(elm));

The above local function declares two parameters. The first (a) is array (IN/OUT) and the second (Value) is function parameter.

In the declaration of a function parameter we declare its parameters the same way we do for the declaration of any local function. In the above example we declare a function parameter with one simple parameter (elm). MySort will accept in this parameter any local function with an interface of one simple parameter.

function TextValue(elm);
 Left(elm,3);
end;

MySort(AnyArr, TextValue);

The parentheses used to declare the interface of a function parameter help CalcIt language to distinguish a function parameter from a simple one. So, even the function parameter do not use any parameters at all we have to supply at least an empty set of parentheses.

In the body of a local function which declares function parameters, these parameters are used the same way we make calls to local functions. Every time the local function passed to such parameters is called. 

The local function passed to a function parameter can be created in any place of the code, before or later of the point of  its reference. When the local function is referred before its creation in the code then CalcIt automatically creates a forward declaration. See below on the subject of forward declarations.

Function parameters is a way to create generalized functions customized by a set of routines provided in their parameter list. The real value of this feature is in case of UDF functions, Class constructors and Class public functions.

Similarly to declare the rest of the types as parameters, parameter names are preceded with the following keywords:

  • Buffer parameters with keyword buf.
  • File parameters with keyword file.
  • Form parameters with keyword form.
  • Class parameters with the name of the appropriate Class definition (already existed in Classes list).

NOTE: Parameters of type File, Class and Form are always IN/OUT (BY REFERENCE). The symbol & to denote IN/OUT parameter passing is optional in this case.

We can make forward declarations and implement the function later in our code using the command forward:

function AnyFuncName(a,b,c);forward; //declared as forward
//
any code here that call or refers to AnyFuncName
...
...
...
...
//implemented here
function AnyFuncName(a,b,c);
(a+b+c)*10;
end;

See also PROPERTY

 

Go Back