Toggle menu
876
3.8K
30.2K
279.1K
Catglobe Wiki
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Function class: Difference between revisions

From Catglobe Wiki
Tungocman (talk | contribs)
No edit summary
No edit summary
 
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<p style="color:#000099; font-size:14px;"><strong>Function : The function object</strong></p>
{{CGscriptClass_Template
|Name=<nowiki>Function</nowiki>
|Description=<nowiki>The function object</nowiki>
|InheritsFrom=object|Constructors=
{{CGscriptConstructors_Template|Parameters=
{{CGscriptParameters_Template|Type=string|Name=<nowiki>functionName</nowiki>|Description=<nowiki>Name of the variable defined as a function</nowiki>}}
|Description=<nowiki>Create a new function that will reuse an existing function</nowiki>}}
|Methods=
{{CGscriptMethods_Template|ReturnType=object|Name=<nowiki>Call</nowiki>|Parameters={{CGscriptParameters_Template|Type=params AnyType|Name=params AnyType|Description=}}
|Description=<nowiki>Evaluate a function with positional parameters.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=object|Name=<nowiki>Invoke</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=array|Name=<nowiki>param</nowiki>|Description=<nowiki>The parameters given to the function mapped to the position used in the definition</nowiki>}}
|Description=<nowiki>Evaluate a function with positional parameters.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=object|Name=<nowiki>Invoke</nowiki>|Parameters=
{{CGscriptParameters_Template|Type=Dictionary|Name=<nowiki>param</nowiki>|Description=<nowiki>The parameters given to the function mapped to the names used in the definition</nowiki>}}
|Description=<nowiki>Evaluate a function with named parameters.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=string|Name=<nowiki>ToString</nowiki>|Inherited=object|Description=<nowiki>The string representation of the object.</nowiki>}}
|Properties=
{{CGscriptProperties_Template|ReturnType=string|Name=<nowiki>ObjectTypeName</nowiki>|HasGetter=1|Description=<nowiki>The name of the type of object.</nowiki>}}
{{CGscriptProperties_Template|ReturnType=TypeInformation|Name=<nowiki>TypeInformation</nowiki>|HasGetter=1|Inherited=object|Description=<nowiki>Get information about this class.</nowiki>}}
}}


&nbsp;
==== <span style="color:#a52a2a;">'''Scoping of variables'''</span> ====
 
<span style="color:#a52a2a;">'''Constructors'''</span>
 
*<span style="color:#000000;">'''(string functionName)''' - Create a new function that will reuse an existing function.</span>
 
&nbsp;<span style="color:#a52a2a;">'''Methods'''</span>
 
*<span style="color:#000000;">'''AnyType Call(Params AnyType)''' - Evaluate a function with positional parameters.</span>
*<span style="color:#000000;">'''AnyType Invoke(array parameter)''' - Evaluate a function with positional parameters.</span>
*<span style="color:#000000;">'''AnyType Invoke(Dictionary parameter)''' - Evaluate a function with named parameters.</span>
*<span style="color:#000000;">'''string ToString()''' - The string representation of the object.</span>
 
&nbsp;<span style="color:#a52a2a;">'''Properties'''</span>
 
*<span style="color:#000000;">'''string ObjectTypeName HasGetter'''&nbsp; - The name of the type of object.</span>
*<span style="color:#000000;">'''TypeInformation TypeInformation HasGetter'''&nbsp; - Get information about this class.</span>
 
&nbsp;
 
<span style="color:#a52a2a;">'''Scoping of variables'''</span>


<span style="color:#a52a2a;">'''Ex1:'''</span>  
<span style="color:#a52a2a;">'''Ex1:'''</span>  


function a = function ()  
<source lang="javascript">object a = function ()
 
{
{  
  number b = 1;
 
  return true;
&nbsp;&nbsp; number b = 1;  
};
 
print(b);             // Not work, get error: Undefined variable name: 'b'. @</source>  
&nbsp;&nbsp; return true;  
 
};  
 
print(b);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;<span style="color:#006400;">&nbsp; // Not work, get error: Undefined variable name: 'b'. @</span>  


<span style="color:#a52a2a;">'''Ex2:'''</span>  
<span style="color:#a52a2a;">'''Ex2:'''</span>  


number i = 10;  
<source lang="javascript">number i = 10;
 
object a = function () {
function a = function () {  
  print(i);
 
  print(b);
&nbsp; print(i);  
};
 
number b = 20;
&nbsp; print(b);  
a.Call();         // 10 20
 
Function f = new Function("a");   // Create a function f by using the constructor of this Function class
};  
f.Call();          // 10 20
 
Function x = a;
number b = 20;  
x.Call();          // 10 20
 
a.Call();&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#006400;">&nbsp;// 10 20</span>
 
<span style="color:#a52a2a;">'''Ex3:'''</span>
 
number i = 10;  
 
function a = function () {


&nbsp; print(i);
</source>


&nbsp; print(b);  
<span style="color: rgb(165, 42, 42);">'''Ex3:'''</span>


};  
<source lang="javascript">number i = 10;
object a = function () {
  print(i);
  print(b);
};
a.Call();              // Not work, get error: Undefined variable name: 'b'. @
number b = 20;


a.Call();&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#006400;">// Not work, get error: Undefined variable name: 'b'. @</span>  
</source>  
 
number b = 20;


<span style="color:#a52a2a;">'''Ex4:'''</span>  
<span style="color:#a52a2a;">'''Ex4:'''</span>  


number i = 10;  
<source lang="javascript">number i = 10;
object a = function (number i) {
  print(i);
};
a.Call(100);                    // 100
print(i);                      // 10
Function f = new Function("a"); // Create a function f by using the constructor of this Function class
f.Call(100);                    // 100
print(i);                      // 10


function a = function (number i) {
</source>  
 
&nbsp; print(i);
 
};
 
a.Call(100);&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#006400;">&nbsp; // 100</span>
 
print(i);&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#006400;"> // 10</span>  


<span style="color:#a52a2a;">'''Ex5:'''</span>  
<span style="color:#a52a2a;">'''Ex5:'''</span>  


number i = 10;  
<source lang="javascript">number i = 10;
 
object a = function () {
function a = function () {  
  number i = 100;
 
  print(i);
&nbsp; number i = 100;  
};
 
a.Call();        // Not work, get error:  Illegal variable re-declaration @</source> &nbsp;  
&nbsp; print(i);  
 
};  


a.Call();&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#006400;"> // Not work, get error:&nbsp; Illegal variable re-declaration @</span>  
<br>


&nbsp;
==== <span style="color:#a52a2a;">'''Recursion'''</span> ====
 
<span style="color:#a52a2a;">'''Recursion'''</span>  


<span style="color:#a52a2a;">'''Ex1:'''</span>  
<span style="color:#a52a2a;">'''Ex1:'''</span>  


function a = function (number i)  
<source lang="javascript">object a = function (number i)
 
{
{  
if (i == 0) return;
 
a.Call(i - 1);
&nbsp;if (i == 0) return;  
return i;
 
};
&nbsp;a.Call(i - 1);  
a.Call(49);           // Return 49
 
Function f = new Function("a"); // Create a function f by using the constructor of this Function class
&nbsp;return i;  
f.Call(49);            // Return 49
 
</source>
};  
 
a.Call(49); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; <span style="color:#006400;">// Return 49</span>  


<span style="color:#a52a2a;">'''Ex2:'''</span>  
<span style="color:#a52a2a;">'''Ex2:'''</span>  


function a = function (number i)  
<source lang="javascript">object a = function (number i){
 
if (i == 0) return;
{  
a.Call(i - 1);
 
return i;
&nbsp;if (i == 0) return;  
};
 
a.Call(50);           // get error because the maximum limit of recursion is: 49</source>  
&nbsp;a.Call(i - 1);  
 
&nbsp;return i;  
 
};  
 
a.Call(50);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; <span style="color:#006400;">// get error because the maximum limit of recursion is: '''49'''</span>  
 
&nbsp;
 
<span style="color:#a52a2a;">'''Examples'''</span>
 
function x = function (number a, number b, number c = 10)
 
{
 
&nbsp;&nbsp; number d = 1;
 
&nbsp;&nbsp; return a + b + c + d + p;
 
};
 
number p = 10;
 
&nbsp;
 
print(x.Call(1,2));
 
print(x.Call(1,2,3));
 
&nbsp;
 
function y = x;
 
print(y.Call(1,2));
 
print(y.Call(1,2,3));
 
&nbsp;
 
function z = new Function("x");&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#006400;">// remmember that new '''F'''unction("x") NOT new function("x")</span>
 
print(z.Call(1,2));
 
print(z.Call(1,2,3));
 
&nbsp;
 
function f;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#006400;">// will be empty</span>
 
Function ff;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#006400;">//&nbsp; get error because there is no default constructor</span>
 
&nbsp;
 
array paraArray_a = {1,2};
 
array paraArray_b = {1,2,3};


print(x.Invoke(paraArray_a));
<br>


print(x.Invoke(paraArray_b));
==== <span style="color:#a52a2a;">'''Send function to Called Workflow'''</span> ====
 
&nbsp;
 
Dictionary dic1 = {"a": 1,"b": 2};
 
print(x.Invoke(dic1));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#006400;">// 24</span>
 
&nbsp;
 
Dictionary dic2 = {"a": 1,"b": 2,"c": 3};
 
print(x.Invoke(dic2));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:#006400;">// 17</span>
 
&nbsp;
 
Dictionary dic3 = {"c": 3,"a": 1,"b": 2};
 
print(x.Invoke(dic3));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#006400;"> // 17</span>
 
&nbsp;
 
Dictionary dic = {"para_1": 1,"para_2": 2,"para_3": 3};
 
print(x.Invoke(dic));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;<span style="color:#006400;">&nbsp; //&nbsp;&nbsp;&nbsp; will cause error because the keys in dictionary not the same as name of parameters which is defined in function x</span>
 
&nbsp;
 
<span style="color:#a52a2a;">'''Send function to Called Workflow'''</span>  


<span style="color:#a52a2a;">'''Ex1:'''</span>  
<span style="color:#a52a2a;">'''Ex1:'''</span>  


<span style="font-size:10px;"><span style="color: rgb(0, 100, 0);">// IN FIRST WORKFLOW</span></span>
<source lang="javascript">// IN FIRST WORKFLOW
 
object x = function (number a, number b) {
function x = function (number a, number b) {  
return a + b;
 
};
return a + b;  
Workflow_call(37248984, {x});
 
};  
 
Workflow_call(37248984, {x});  
 
<span style="font-size:10px;"><span style="color: rgb(0, 100, 0);">//&nbsp; IN SECOND (CALLED) WORKFLOW (Resource Id: 37248984)</span></span>
 
array Workflow_parameters = Workflow_getParameters();
 
return Workflow_parameters[0].Call(1,2);
 
<span style="font-size:10px;"><span style="color: rgb(0, 100, 0);">// ------get result: 3---------</span></span>
 
<span style="color:#a52a2a;">'''Ex2:'''</span>
 
<span style="font-size:10px;"><span style="color: rgb(0, 100, 0);">// IN FIRST WORKFLOW</span></span>


number i = 10;  
//  IN SECOND (CALLED) WORKFLOW (Resource Id: 37248984)
array Workflow_parameters = Workflow_getParameters();
return Workflow_parameters[0].Call(1,2);
// ------get result: 3---------</source><br> <span style="color:#a52a2a;">'''Ex2:'''</span>


function x = function (number a, number b) {  
<source lang="javascript">// IN FIRST WORKFLOW
number i = 10;
object x = function (number a, number b) {
return a + b + i;
};
Workflow_call(37248984, {x});


return a + b + i;  
// IN SECOND (CALLED) WORKFLOW (Resource Id: 37248984)
array Workflow_parameters = Workflow_getParameters();
return Workflow_parameters[0].Call(1,2);
// ------get result: 13---------</source>


};
<br>


Workflow_call(37248984, {x});  
==== <span style="color:#a52a2a;">'''Examples'''</span>  ====


<span style="font-size:10px;"><span style="color: rgb(0, 100, 0);">// IN SECOND (CALLED) WORKFLOW (Resource Id: 37248984)</span></span>
<source lang="javascript">object x = function (number a, number b, number c = 10)
{
  number d = 1;
  return a + b + c + d + p;
};
number p = 10;
print(x.Call(1,2));
print(x.Call(1,2,3));


array Workflow_parameters = Workflow_getParameters();  
object y = x;
print(y.Call(1,2));
print(y.Call(1,2,3));
object z = new Function("x");        // remmember that new Function("x") NOT new function("x")
print(z.Call(1,2));
print(z.Call(1,2,3));


return Workflow_parameters[0].Call(1,2);  
object f;                             // will be empty
object ff;                            //  get error because there is no default constructor


<span style="font-size:10px;"><span style="color: rgb(0, 100, 0);">// ------get result: 13---------</span></span>  
array paraArray_a = {1,2};
array paraArray_b = {1,2,3};
print(x.Invoke(paraArray_a));
print(x.Invoke(paraArray_b));</source>  


&nbsp;  
<br> <source lang="javascript">Dictionary dic1 = {"a": 1,"b": 2};
print(x.Invoke(dic1));                                    // 24
Dictionary dic2 = {"a": 1,"b": 2,"c": 3};
print(x.Invoke(dic2));                                    // 17


&nbsp;  
Dictionary dic3 = {"c": 3,"a": 1,"b": 2};
print(x.Invoke(dic3));                                    // 17


[[Category:Data_Types_Literals_and_Variables]]
Dictionary dic = {"para_1": 1,"para_2": 2,"para_3": 3};
print(x.Invoke(dic));            //  will cause error because the keys in dictionary not the same as name of parameters which is defined in function x</source><br>

Latest revision as of 06:48, 2 July 2020

Function



The function object

Parent class

Inherits from object

Constructors

  • (string functionName "Name of the variable defined as a function") - Create a new function that will reuse an existing function

Methods

  • object Call(params AnyType) - Evaluate a function with positional parameters.
  • object Invoke(array param "The parameters given to the function mapped to the position used in the definition") - Evaluate a function with positional parameters.
  • object Invoke(Dictionary param "The parameters given to the function mapped to the names used in the definition") - Evaluate a function with named parameters.
  • (From object) string ToString() - The string representation of the object.

Properties

  • string ObjectTypeName { get; } - The name of the type of object.
  • (From object) TypeInformation TypeInformation { get; } - Get information about this class.


Scoping of variables

Ex1:

object a = function ()
{
   number b = 1;
   return true;
};
print(b);             // Not work, get error: Undefined variable name: 'b'. @

Ex2:

number i = 10;
object a = function () {
  print(i);
  print(b);
};
number b = 20;
a.Call();          // 10 20
Function f = new Function("a");   // Create a function f by using the constructor of this Function class
f.Call();          // 10 20
Function x =  a;
x.Call();          // 10 20

Ex3:

number i = 10;
object a = function () {
  print(i);
  print(b);
};
a.Call();              // Not work, get error: Undefined variable name: 'b'. @
number b = 20;

Ex4:

number i = 10;
object a = function (number i) {
  print(i);
};
a.Call(100);                    // 100
print(i);                       // 10
Function f = new Function("a"); // Create a function f by using the constructor of this Function class
f.Call(100);                    // 100
print(i);                       // 10

Ex5:

number i = 10;
object a = function () {
  number i = 100;
  print(i);
};
a.Call();        // Not work, get error:  Illegal variable re-declaration @

 


Recursion

Ex1:

object a = function (number i)
{
 if (i == 0) return;
 a.Call(i - 1);
 return i;
};
a.Call(49);            // Return 49
Function f = new Function("a"); // Create a function f by using the constructor of this Function class
f.Call(49);            // Return 49

Ex2:

object a = function (number i){
 if (i == 0) return;
 a.Call(i - 1);
 return i;
};
a.Call(50);           // get error because the maximum limit of recursion is: 49


Send function to Called Workflow

Ex1:

// IN FIRST WORKFLOW
object x = function (number a, number b) {
return a + b;
};
Workflow_call(37248984, {x});

//  IN SECOND (CALLED) WORKFLOW (Resource Id: 37248984)
array Workflow_parameters = Workflow_getParameters();
return Workflow_parameters[0].Call(1,2);
// ------get result: 3---------


Ex2:

// IN FIRST WORKFLOW
number i = 10;
object x = function (number a, number b) {
return a + b + i;
};
Workflow_call(37248984, {x});

// IN SECOND (CALLED) WORKFLOW (Resource Id: 37248984)
array Workflow_parameters = Workflow_getParameters();
return Workflow_parameters[0].Call(1,2);
// ------get result: 13---------


Examples

object x = function (number a, number b, number c = 10)
{
   number d = 1;
   return a + b + c + d + p;
};
number p = 10;
 
print(x.Call(1,2));
print(x.Call(1,2,3));

object y = x;
print(y.Call(1,2));
print(y.Call(1,2,3));
 
object z = new Function("x");         // remmember that new Function("x") NOT new function("x")
print(z.Call(1,2));
print(z.Call(1,2,3));

object f;                             // will be empty
object ff;                            //  get error because there is no default constructor

array paraArray_a = {1,2};
array paraArray_b = {1,2,3};
print(x.Invoke(paraArray_a));
print(x.Invoke(paraArray_b));


Dictionary dic1 = {"a": 1,"b": 2};
print(x.Invoke(dic1));                                     // 24
 
Dictionary dic2 = {"a": 1,"b": 2,"c": 3};
print(x.Invoke(dic2));                                     // 17

Dictionary dic3 = {"c": 3,"a": 1,"b": 2};
print(x.Invoke(dic3));                                     // 17

Dictionary dic = {"para_1": 1,"para_2": 2,"para_3": 3};
print(x.Invoke(dic));            //  will cause error because the keys in dictionary not the same as name of parameters which is defined in function x