Difference between revisions of "Function class"
Line 1: | Line 1: | ||
− | + | === '''Function : The function object''' === | |
| | ||
Line 21: | Line 21: | ||
| | ||
− | <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> | ||
+ | <source lang="javascript">function a = function () | ||
+ | { | ||
+ | number b = 1; | ||
+ | return true; | ||
+ | }; | ||
+ | print(b); // Not work, get error: Undefined variable name: 'b'. @</source> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<span style="color:#a52a2a;">'''Ex2:'''</span> | <span style="color:#a52a2a;">'''Ex2:'''</span> | ||
− | number i = 10; | + | <source lang="javascript">number i = 10; |
+ | function a = function () { | ||
+ | print(i); | ||
+ | print(b); | ||
+ | }; | ||
+ | number b = 20; | ||
+ | a.Call(); // 10 20</source><span style="color:#a52a2a;"</span><br> | ||
− | + | <span style="color: rgb(165, 42, 42);">'''Ex3:'''</span> | |
− | |||
− | + | <source lang="javascript">number i = 10; | |
+ | function a = function () { | ||
+ | print(i); | ||
+ | print(b); | ||
+ | }; | ||
+ | a.Call(); // Not work, get error: Undefined variable name: 'b'. @ | ||
+ | number b = 20;</source> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<span style="color:#a52a2a;">'''Ex4:'''</span> | <span style="color:#a52a2a;">'''Ex4:'''</span> | ||
+ | <source lang="javascript">number i = 10; | ||
+ | function a = function (number i) { | ||
+ | print(i); | ||
+ | }; | ||
+ | a.Call(100); // 100 | ||
+ | print(i); // 10</source> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<span style="color:#a52a2a;">'''Ex5:'''</span> | <span style="color:#a52a2a;">'''Ex5:'''</span> | ||
+ | <source lang="javascript">number i = 10; | ||
+ | function a = function () { | ||
+ | number i = 100; | ||
+ | print(i); | ||
+ | }; | ||
+ | a.Call(); // Not work, get error: Illegal variable re-declaration @</source> | ||
+ | | ||
− | + | <span style="color: rgb(165, 42, 42);"</span><br> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | ==== <span style="color:#a52a2a;">'''Recursion'''</span> ==== | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | <span style="color:#a52a2a;">'''Recursion'''</span> | ||
<span style="color:#a52a2a;">'''Ex1:'''</span> | <span style="color:#a52a2a;">'''Ex1:'''</span> | ||
+ | <source lang="javascript">function a = function (number i) | ||
+ | { | ||
+ | if (i == 0) return; | ||
+ | a.Call(i - 1); | ||
+ | return i; | ||
+ | }; | ||
+ | a.Call(49); // Return 49</source> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<span style="color:#a52a2a;">'''Ex2:'''</span> | <span style="color:#a52a2a;">'''Ex2:'''</span> | ||
+ | <source lang="">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</source> | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
| | ||
− | <span style="color:#a52a2a;">'''Examples'''</span> | + | ==== <span style="color:#a52a2a;">'''Examples'''</span> ==== |
− | + | <source lang="javascript">function x = function (number a, number b, number c = 10) | |
− | 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)); | |
− | }; | ||
− | |||
− | number p = 10; | ||
− | |||
− | |||
− | print(x.Call(1,2)); | + | 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));</source> | ||
− | |||
− | print( | + | <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 | ||
− | print( | + | 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</source><br> | ||
− | |||
− | |||
− | + | ==== <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> | ||
+ | <source lang="javascript">// 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---------</source><br> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | array Workflow_parameters = Workflow_getParameters(); | ||
− | |||
− | return Workflow_parameters[0].Call(1,2); | ||
− | |||
− | |||
− | |||
<span style="color:#a52a2a;">'''Ex2:'''</span> | <span style="color:#a52a2a;">'''Ex2:'''</span> | ||
+ | <source lang="javascript">// 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---------</source><br> | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | array Workflow_parameters = Workflow_getParameters(); | ||
− | |||
− | return Workflow_parameters[0].Call(1,2); | ||
− | |||
− | |||
− | |||
| | ||
Revision as of 09:03, 14 September 2011
Contents
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
<span style="color:#a52a2a;"
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 @
<span style="color: rgb(165, 42, 42);"
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---------