A function can "discover" its argument list from the arg%function variable. However, "function" is itself an argument, which contains the function name. If you are not interested in the function name, you can ignore it (or clear it before you loop through the argument list using "foreach"):
function sumup {
var result%function = 0;
clear arg%function:function; // Clear the function name
foreach (arg%function) var result%function conv="+=:{foreach}";
display "{arg%function|list:(@key) + ()} = ";
display "{arg%function|list:(@value) + ()} = ";
display "{result%function}<br>\n";
}
sumup a=1 b=2 c=3; // Output: a + b + c = 1 + 2 + 3 = 6
display "The sum is {result%sumup}<br>\n"; // Output: The sum is 6
Besides the special arg%function:function argument, if the function is called as a method of an object, you will have arg%function:this (the object name in the parent scope) and arg%function:thisfunction (the function name in the parent scope, which can be different from the one in the declaration line, denoted by arg%function:function.)
Every BEE Variable is an array. An argument is no different. In the example of the function "sumup", you can pass an array of numbers instead of separate numbers.
function sumup {
var result%function = 0;
foreach (arg%numbers) var result%function conv="+=:{foreach}";
display "{arg%numbers|list:(@key) + ()} = ";
display "{arg%numbers|list:(@value) + ()} = ";
display "{result%function}<br>\n";
}
sumup numbers="(array)a=>1,b=>2,c=>3"; // Output: a + b + c = 1 + 2 + 3 = 6
display "The sum is {result%sumup}<br>\n"; // Output: The sum is 6
You may use a variable in the calling line:
var myArr = "(array)a=>1,b=>2,c=>3";
sumup numbers="(var)myArr"; // Output: a + b + c = 1 + 2 + 3 = 6
|