Like all variables, the "result" variable is an array too. You can pass more than one value via the elements of result%function if need to.
In the following example, five result values are returned in one go. Notes: 1. you can do anything to result%function like you do with other variables without causing a function exit or incurring performance penalty; 2. "{arg%function:function} is the function name, used to access the nameless argument in the calling line.
// Returns sum, first, last, max and min
function give_me_5 {
var argvar = {arg%function:function};
if ({#arg%{argvar}} == 0) return;
var result%function:sum = 0;
var result%function:first = "{arg%{argvar}|shift}";
var result%function:last = "{arg%{argvar}|pop}";
var result%function:max = "{result%function:first}";
var result%function:min = "{result%function:first}";
foreach (arg%{argvar}) {
var result%function:sum conv="+=:{foreach}";
if ('{foreach}' > '{result%function:max}')
var result%function:max = {foreach};
if ('{foreach}' < '{result%function:min}')
var result%function:min = {foreach};
}
}
give_me_5 "(array)5,3,2,6,9,7,6,8";
display "{result%give_me_5|list}";
// Output: sum=>'46',first=>'5',last=>'8',max=>'9',min=>'2'
Also, two special purpose variables "status%function" and "message%function" are there for you to pass out error code and error message. As a de facto standard, 0 status and blank message indicate no error.
Following the previous example, you can set status and message before returning.
// Returns sum, first, last, max and min
function give_me_5 {
var status%function = 1; // Assume guilty until proven innocent
var message%function = "argument not specified";
var argvar = {arg%function:function};
if ({#arg%{argvar}} == 0) return;
var result%function:sum = 0;
var result%function:first = "{arg%{argvar}|shift}";
var result%function:last = "{arg%{argvar}|pop}";
var result%function:max = "{result%function:first}";
var result%function:min = "{result%function:first}";
foreach (arg%{argvar}) {
var result%function:sum conv="+=:{foreach}";
if ('{foreach}' > '{result%function:max}')
var result%function:max = {foreach};
if ('{foreach}' < '{result%function:min}')
var result%function:min = {foreach};
}
var status%function = 0;
var message%function = "";
}
The last two lines can be replaced by return status=0 message="";
|