img

User Functions

Function - a piece of code or algorithm that performs a specific sequence of operations. Each function may include:

  • function name
  • return value
  • formal parameters
  • function body

Example:

 int multiplication(int a, int b) { return a*b; } 

Function name - "multiplication" ; return value - int , formal parameters "a" and "b" (both int types), function body consists of a single operator - return a*b;

Parameters can be passed into the function both by value and by reference. When parameters are passed by value, they do not change after the execution of the function.

Example:

 int Initialize() {return(0);} int Run() { int a=1; changeA(a); System.Print("a="+a); return(0); } void changeA(int a) { a=2; } int DeInitialize(){return(0);} 

Output:

a=1

Parameters can be passed to function by reference. In that case, after the function is executed, parameters values may change. For this purpose the keyword &out must be specified between the parameter type and its name.

 int Initialize() {return(0);} int Run() { int a=1; changeA(a); System.Print("a="+a); return(0); } void changeA(int &out a) { a=2; } int DeInitialize(){return(0);} 

Output:

a=2

The parameters passed to the function can have default values. If any parameter is set by default, then all subsequent parameters should be set by default.

Example:

 int Initialize() {return(0);} int Run() { System.Print("sum1="+sum(2)); System.Print("sum2="+sum(10,20,30)); return(0); } int sum(int a, int b=3, int c=4) { return a+b+c; } int DeInitialize(){return(0);} 

Output:

 sum1 = 9 sum2 = 60 

The function can return a reference to a global variable. Using this reference, recording and reading the value of the global variable may be performed.

 extern int a1=0; extern int a2=0; extern int a3=0; extern int a4=0; extern int a5=0; int Initialize() {return(0);} int Run() { for(int i=0;i<5;i++) { changeGlobal(i)=7; } for(int i=0;i<5;i++) { System.Print("i="+i+" value="+changeGlobal(i)); } return(0); } int DeInitialize(){return(0);} int& changeGlobal(int i) { switch(i) { case 1: return a1; case 2: return a2; case 3: return a3; case 4: return a4; case 5: return a5; } return a5; } 

Output:

 i=0 value=7 i=1 value=7 i=2 value=7 i=3 value=7 i=4 value=7 

For convenience of use of user functions, it is allowed to group and put them into individual files. These files, if necessary, should be included by using the directive #include "folder\filename".

Example:
Let there be a compiled io.ntl file , which is located in the Advisors folder:

 void Print(array <int> a, string separator=" ") { string data=""; for(uint i=0;i<a.length();i++) { data+=a[i]; if(i<a.length()-1) data+=separator; } System.Print(data); } 

The script ArrayExample.ntl uses the Print function, defined in io.ntl:

 #include "Advisors\io.ntl" int Initialize() { return(0); } int Run() { array<int> a = {22,11,33}; Print(a); return(0); } int DeInitialize() { return(0); }