Subscribe

  • Subscribe  

Can I have a variable number of arguments in AS3?

Posted by Jay | April 14, 2008 .

Having programmed in Java extensively before, I am used to the various features of Java that I take them for granted. Even today, when I first think of the logic for a program, it is based on what can be done in Java and what can’t. So, when I had to use ActionScript for a client, I felt a little disoriented (But naturally!) And that’s when I started trying to figure out how to use the features of Java that I am so accustomed to, for instance, the varargs functionality, in AS3. Whether such features existed in AS3 or whether I needed to adapt my programming to a whole new set of features? To my relief, I found that most of the Java features that I was used to were part of AS3 too.

Getting back to the example mentioned above, I figured out that an AS3 function can be defined to take a variable number of arguments similar to the varargs functionality of Java. What’s more is that there are two ways to accomplish the same: one by using the (rest) parameter and the other by using the arguments object.

I’ll give you a sample of the first method:

private function addition(... args):Number{

  var sum:Number = 0;
  for(var i:int = 0; i<args.length;i++){
     sum += args[i];
  }
  return sum;
}

Now all the following calls can be used without the hassle of redefining the function

  var a:Number = addition(10,50);

  a = addition(10,50,100);
  a = addition(10,50,100,500);
  a = addition(10,50,100,500,1000);

This little discovery definitely took some stress off me. I hope it helps you too.

Leave a Comment

If you would like to make a comment, please fill out the form below.


Name

Email

Website

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Comments

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word


Related Posts

10 Comments so far
  1. Anonymous  December 12, 2008 2:51 am

    outstanding! But what if the variable arguments are not of the same Type?

  2. Alok Joshi  February 19, 2009 12:15 pm

    What about the second method?

  3. Linda  January 31, 2010 9:56 am

    this is amazing and very helpful. Thanks.

  4. Anonymous  June 7, 2010 3:25 am

    how can forward args to another method? i try but it treats args as one array variable.

    something likes:
    private function addition(…args)
    {
    addition2(args);
    }

    private function addition2(…args)
    {
    var sum:Number = 0;
    for(var i:int = 0; i<args.length;i++){
    sum += args[i];
    }
    return sum;
    }

  5. Anonymous  June 28, 2010 1:20 pm

    cool, thanks

  6. miles  June 28, 2010 1:22 pm

    “But what if the variable arguments are not of the same Type?”

    if(typeof args[i] is int){
    //action a
    }else if(typeof args[i] is String){
    //action b
    }

  7. Raphi  December 16, 2010 7:31 pm

    Thanks for this simple but useful post !

    Maybe for each is a little more friendly:

    private function addition(… args):Number{

    var sum:Number = 0;
    for each (var arg:int in args){
    sum += arg;
    }
    return sum;
    }

  8. bruce  April 26, 2011 9:22 am

    “how can forward args to another method?”

    You would need to change your code to this:

    private function addition(…args)
    {
    addition2.apply( null, args );
    }

    private function addition2(…args)
    {
    var sum:Number = 0;
    for(var i:int = 0; i<args.length;i++){
    sum += args[i];
    }
    return sum;
    }

  9. Alexey  June 6, 2011 5:20 am

    Thank you very much!

  10. codah  June 13, 2011 5:42 am

    We just updated our code from AS2 to AS3 and this was really helpful, thanks mate!

<

Direct TV Offers - usdirect has the best directtv deals