New Comment System@Siebel Unleashed.. Do Try and give me feedback | Next Tip »Home

Variable argument functions

OkAvarageGoodVery GoodExcellent (2 votes, average: 4.50 out of 5)
Loading ... Loading ...

I am not sure how many of you believed that if we declare a function like this

function SumAll(){}

then you cannot pass any argument to it (I believed it .. till few days back). On the contrary to the general belief these functions are known as variable argument functions and can accept any number of arguments that are passed to them, which means we call this function in any of the ways mentioned below.

SumAll(1,3,5,6,7,8);
SumAll();
SumAll(4,7,8);

Now, you may ask me a question, what good a function is if I don’t know what is going to be passed it and how do I process these arguments. The answer to this question is arguments property ( :) confused). Ok, Ok let’s not waste your precious time anymore.

Siebel implicitly passes an argument property which is an array called “arguments” to these functions. The first variable passed to a function is referred to as arguments[0], the second as arguments[1], and so forth.

This property allows us to have a function with indefinite number of arguments. Here is a sample function to explain the working of this property.

function SumAll()
{
var total = 0;
for (var i = 0; i < arguments.length; i++)
{
total += arguments[i];
}
return total;
}

The above mentioned function will return total of all the arguments supplied to it.

You can also pass complex object types such as Property Set, Arrays etc as these are passed by reference to the functions. Now, use your imagination to make use of this property.

P.S: I am trying to use this property to lay foundation of an Error Handling Framework ;)

  • Share/Bookmark

Related Posts


Article by neel

Authors bio is coming up shortly. neel tagged this post with: Read 387 articles by neel
  • Hi Neel,

    It is important to note that the 'arguments' object is deprecated in the Siebel ST Engine and there is no official replacement.

    If you used the arguments object to dynamically get the name of the current function, it will not work anymore, you'll have to hard code it.

    Jason
  • Siddique
    Please post the Error handling Framework. It might be of great help.

    -- Siddique
  • toxcct
    that's a true thing, and nice to remind it.

    also, as eScript is actually ECMAScript, doing Object Oriented Programming (pseudo OOP in fact, but anyway) like in javascript is fully possible, just the same way.

    An article about this could be interresting Neel :)
blog comments powered by Disqus

Polls

Do you like the new Comment System and new look to Site?

View Results

Loading ... Loading ...