Function class: Difference between revisions
More actions
Created page with "'''Function : The function object'''  <span style="color:#a52a2a;">'''Constructors'''</span> *<span style="color:#000000;">'''(string functionName)''' - Create a ne..."  |
No edit summary |
||
| Line 1: | Line 1: | ||
<p style="color:#000099; font-size:14px;"><strong>Function : The function object</strong></p> | |||
| Â | ||
<span style="color:#a52a2a;">'''Constructors'''</span> | <span style="color:#a52a2a;">'''Constructors'''</span> Â | ||
*<span style="color:#000000;">'''(string functionName)''' - Create a new function that will reuse an existing function.</span> Â | *<span style="color:#000000;">'''(string functionName)''' - Create a new function that will reuse an existing function.</span> | ||
<span style="color:#a52a2a;">'''Methods'''</span> | <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 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(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;">'''AnyType Invoke(Dictionary parameter)''' - Evaluate a function with named parameters.</span> Â | ||
*<span style="color:#000000;">'''string ToString()''' - The string representation of the object.</span> Â | *<span style="color:#000000;">'''string ToString()''' - The string representation of the object.</span> | ||
<span style="color:#a52a2a;">'''Properties'''</span> | <span style="color:#a52a2a;">'''Properties'''</span> Â | ||
*<span style="color:#000000;">'''string ObjectTypeName HasGetter''' - The name of the type of object.</span> Â | *<span style="color:#000000;">'''string ObjectTypeName HasGetter''' - The name of the type of object.</span> Â | ||
*<span style="color:#000000;">'''TypeInformation TypeInformation HasGetter''' - Get information about this class.</span> Â | *<span style="color:#000000;">'''TypeInformation TypeInformation HasGetter''' - Get information about this class.</span> | ||
| Â | ||
<span style="color:#a52a2a;">'''Scoping of variables'''</span> | <span style="color:#a52a2a;">'''Scoping of variables'''</span> Â | ||
<span style="color:#a52a2a;">'''Ex1:'''</span> | <span style="color:#a52a2a;">'''Ex1:'''</span> Â | ||
function a = function () | function a = function () Â | ||
{ | { Â | ||
number b = 1; | number b = 1; Â | ||
return true; | return true; Â | ||
}; | }; Â | ||
print(b); <span style="color:#006400;"> // Not work, get error: Undefined variable name: 'b'. @</span> | print(b); <span style="color:#006400;"> // Not work, get error: Undefined variable name: 'b'. @</span> Â | ||
<span style="color:#a52a2a;">'''Ex2:'''</span> | <span style="color:#a52a2a;">'''Ex2:'''</span> Â | ||
number i = 10; | number i = 10; Â | ||
function a = function () { | function a = function () { Â | ||
print(i); | print(i); Â | ||
print(b); | print(b); Â | ||
}; | }; Â | ||
number b = 20; | number b = 20; Â | ||
a.Call(); <span style="color:#006400;"> // 10 20</span> | a.Call(); <span style="color:#006400;"> // 10 20</span> Â | ||
<span style="color:#a52a2a;">'''Ex3:'''</span> | <span style="color:#a52a2a;">'''Ex3:'''</span> Â | ||
number i = 10; | number i = 10; Â | ||
function a = function () { | function a = function () { Â | ||
print(i); | print(i); Â | ||
print(b); | print(b); Â | ||
}; | }; Â | ||
a.Call(); <span style="color:#006400;">// Not work, get error: Undefined variable name: 'b'. @</span> | a.Call(); <span style="color:#006400;">// Not work, get error: Undefined variable name: 'b'. @</span> Â | ||
number b = 20; | number b = 20; Â | ||
<span style="color:#a52a2a;">'''Ex4:'''</span> | <span style="color:#a52a2a;">'''Ex4:'''</span> Â | ||
number i = 10; | number i = 10; Â | ||
function a = function (number i) { | function a = function (number i) { Â | ||
print(i); | print(i); Â | ||
}; | }; Â | ||
a.Call(100); <span style="color:#006400;"> // 100</span> | a.Call(100); <span style="color:#006400;"> // 100</span> Â | ||
print(i); <span style="color:#006400;"> // 10</span> | print(i); <span style="color:#006400;"> // 10</span> Â | ||
<span style="color:#a52a2a;">'''Ex5:'''</span> | <span style="color:#a52a2a;">'''Ex5:'''</span> Â | ||
number i = 10; | number i = 10; Â | ||
function a = function () { | function a = function () { Â | ||
number i = 100; | number i = 100; Â | ||
print(i); | print(i); Â | ||
}; | }; Â | ||
a.Call(); <span style="color:#006400;"> // Not work, get error: Illegal variable re-declaration @</span> | a.Call(); <span style="color:#006400;"> // Not work, get error: Illegal variable re-declaration @</span> Â | ||
| Â | ||
<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) | function a = function (number i) Â | ||
{ | { Â | ||
if (i == 0) return; | if (i == 0) return; Â | ||
a.Call(i - 1); | a.Call(i - 1); Â | ||
return i; | return i; Â | ||
}; | }; Â | ||
a.Call(49); <span style="color:#006400;">// Return 49</span> | a.Call(49); <span style="color:#006400;">// Return 49</span> Â | ||
<span style="color:#a52a2a;">'''Ex2:'''</span> | <span style="color:#a52a2a;">'''Ex2:'''</span> Â | ||
function a = function (number i) | function a = function (number i) Â | ||
{ | { Â | ||
if (i == 0) return; | if (i == 0) return; Â | ||
a.Call(i - 1); | a.Call(i - 1); Â | ||
return i; | return i; Â | ||
}; | }; Â | ||
a.Call(50); <span style="color:#006400;">// get error because the maximum limit of recursion is: '''49'''</span> | a.Call(50); <span style="color:#006400;">// get error because the maximum limit of recursion is: '''49'''</span> Â | ||
| Â | ||
<span style="color:#a52a2a;">'''Examples'''</span> | <span style="color:#a52a2a;">'''Examples'''</span> Â | ||
function x = function (number a, number b, number c = 10) | function x = function (number a, number b, number c = 10) Â | ||
{ | { Â | ||
number d = 1; | number d = 1; Â | ||
return a + b + c + d + p; | return a + b + c + d + p; Â | ||
}; | }; Â | ||
number p = 10; | number p = 10; Â | ||
| Â | ||
print(x.Call(1,2)); | print(x.Call(1,2)); Â | ||
print(x.Call(1,2,3)); | print(x.Call(1,2,3)); Â | ||
| Â | ||
function y = x; | function y = x; Â | ||
print(y.Call(1,2)); | print(y.Call(1,2)); Â | ||
print(y.Call(1,2,3)); | print(y.Call(1,2,3)); Â | ||
| Â | ||
function z = new Function("x"); <span style="color:#006400;">// remmember that new '''F'''unction("x") NOT new function("x")</span> | function z = new Function("x"); <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)); Â | ||
print(z.Call(1,2,3)); | print(z.Call(1,2,3)); Â | ||
| Â | ||
function f; <span style="color:#006400;">// will be empty</span> | function f; <span style="color:#006400;">// will be empty</span> Â | ||
Function ff; <span style="color:#006400;">// get error because there is no default constructor</span> | Function ff; <span style="color:#006400;">// get error because there is no default constructor</span> Â | ||
| Â | ||
array paraArray_a = {1,2}; | array paraArray_a = {1,2}; Â | ||
array paraArray_b = {1,2,3}; | array paraArray_b = {1,2,3}; Â | ||
print(x.Invoke(paraArray_a)); | print(x.Invoke(paraArray_a)); Â | ||
print(x.Invoke(paraArray_b)); | print(x.Invoke(paraArray_b)); Â | ||
| Â | ||
Dictionary dic1 = {"a": 1,"b": 2}; | Dictionary dic1 = {"a": 1,"b": 2}; Â | ||
print(x.Invoke(dic1)); <span style="color:#006400;">// 24</span> | print(x.Invoke(dic1)); <span style="color:#006400;">// 24</span> Â | ||
| Â | ||
Dictionary dic2 = {"a": 1,"b": 2,"c": 3}; | Dictionary dic2 = {"a": 1,"b": 2,"c": 3}; Â | ||
print(x.Invoke(dic2)); <span style="color:#006400;">// 17</span> | print(x.Invoke(dic2)); <span style="color:#006400;">// 17</span> Â | ||
| Â | ||
Dictionary dic3 = {"c": 3,"a": 1,"b": 2}; | Dictionary dic3 = {"c": 3,"a": 1,"b": 2}; Â | ||
print(x.Invoke(dic3)); <span style="color:#006400;"> // 17</span> | print(x.Invoke(dic3)); <span style="color:#006400;"> // 17</span> Â | ||
| Â | ||
Dictionary dic = {"para_1": 1,"para_2": 2,"para_3": 3}; | Dictionary dic = {"para_1": 1,"para_2": 2,"para_3": 3}; Â | ||
print(x.Invoke(dic)); <span style="color:#006400;"> // will cause error because the keys in dictionary not the same as name of parameters which is defined in function x</span> | print(x.Invoke(dic)); <span style="color:#006400;"> // will cause error because the keys in dictionary not the same as name of parameters which is defined in function x</span> Â | ||
| Â | ||
<span style="color:#a52a2a;">'''Send function to Called Workflow'''</span> | <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> | <span style="font-size:10px;"><span style="color: rgb(0, 100, 0);">// IN FIRST WORKFLOW</span></span> Â | ||
function 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);">// IN SECOND (CALLED) WORKFLOW (Resource Id: 37248984)</span></span> | <span style="font-size:10px;"><span style="color: rgb(0, 100, 0);">// IN SECOND (CALLED) WORKFLOW (Resource Id: 37248984)</span></span> Â | ||
array Workflow_parameters = Workflow_getParameters(); | array Workflow_parameters = Workflow_getParameters(); Â | ||
return Workflow_parameters[0].Call(1,2); | 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="font-size:10px;"><span style="color: rgb(0, 100, 0);">// ------get result: 3---------</span></span> Â | ||
<span style="color:#a52a2a;">'''Ex2:'''</span> | <span style="color:#a52a2a;">'''Ex2:'''</span> Â | ||
<span style="font-size:10px;"><span style="color: rgb(0, 100, 0);">// IN FIRST WORKFLOW</span></span> | <span style="font-size:10px;"><span style="color: rgb(0, 100, 0);">// IN FIRST WORKFLOW</span></span> Â | ||
number i = 10; | number i = 10; Â | ||
function x = function (number a, number b) { | function x = function (number a, number b) { Â | ||
return a + b + i; | return a + b + i; Â | ||
}; | }; Â | ||
Workflow_call(37248984, {x}); | Workflow_call(37248984, {x}); Â | ||
<span style="font-size:10px;"><span style="color: rgb(0, 100, 0);">// IN SECOND (CALLED) WORKFLOW (Resource Id: 37248984)</span></span> | <span style="font-size:10px;"><span style="color: rgb(0, 100, 0);">// IN SECOND (CALLED) WORKFLOW (Resource Id: 37248984)</span></span> Â | ||
array Workflow_parameters = Workflow_getParameters(); | array Workflow_parameters = Workflow_getParameters(); Â | ||
return Workflow_parameters[0].Call(1,2); | return Workflow_parameters[0].Call(1,2); Â | ||
<span style="font-size:10px;"><span style="color: rgb(0, 100, 0);">// ------get result: 13---------</span></span> | <span style="font-size:10px;"><span style="color: rgb(0, 100, 0);">// ------get result: 13---------</span></span> Â | ||
| Â | ||
| Â | ||
[[Category:Data_Types_Literals_and_Variables]] | [[Category:Data_Types_Literals_and_Variables]] | ||
Revision as of 03:53, 14 September 2011
Function : The function object
Â
Constructors
- (string functionName) - Create a new function that will reuse an existing function.
 Methods
- AnyType Call(Params AnyType) - Evaluate a function with positional parameters.
- AnyType Invoke(array parameter) - Evaluate a function with positional parameters.
- AnyType Invoke(Dictionary parameter) - Evaluate a function with named parameters.
- string ToString() - The string representation of the object.
 Properties
- string ObjectTypeName HasGetter - The name of the type of object.
- TypeInformation TypeInformation HasGetter - Get information about this class.
Â
Scoping of variables
Ex1:
function a = function ()
{
  number b = 1;
  return true;
};
print(b);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // Not work, get error: Undefined variable name: 'b'. @
Ex2:
number i = 10;
function a = function () {
 print(i);
 print(b);
};
number b = 20;
a.Call();Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // 10 20
Ex3:
number i = 10;
function a = function () {
 print(i);
 print(b);
};
a.Call();Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // Not work, get error: Undefined variable name: 'b'. @
number b = 20;
Ex4:
number i = 10;
function a = function (number i) {
 print(i);
};
a.Call(100);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // 100
print(i);Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // 10
Ex5:
number i = 10;
function a = function () {
 number i = 100;
 print(i);
};
a.Call();Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // Not work, get error:Â Illegal variable re-declaration @
Â
Recursion
Ex1:
function a = function (number i)
{
 if (i == 0) return;
 a.Call(i - 1);
 return i;
};
a.Call(49); Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // Return 49
Ex2:
function 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
Â
Examples
function 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));
Â
function y = x;
print(y.Call(1,2));
print(y.Call(1,2,3));
Â
function z = new Function("x");Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // remmember that new Function("x") NOT new function("x")
print(z.Call(1,2));
print(z.Call(1,2,3));
Â
function f;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // will be empty
Function 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
Â
Send function to Called Workflow
Ex1:
// IN FIRST WORKFLOW
function 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;
function 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---------
Â
Â