Ultimate Gear War
Join the alien war, prepare your gear and protect your base at all cost!
4.18 / 5.00 15,822 ViewsI want to make a function that can hold infinite parameters.
Like how trace() can go on forever.
Is there a way to do this?
At 5/22/12 02:42 AM, Venks wrote: I want to make a function that can hold infinite parameters.
Like how trace() can go on forever.
Is there a way to do this?
why would you need a function that can hold infinite parameters?
and trace() only holds one. You can concatenate the parameter string with the + symbol, but other than that, it's done.
Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P
You could make a function that takes in an array that would hold your infinite parameters.
At 5/22/12 04:30 AM, 4urentertainment wrote: You could make a function that takes in an array that would hold your infinite parameters.
probably would work for OP's scenario
Beside the point: I still really don't see a use for an infinite number of params
Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P
Check out the ...rest keyword.
At 5/22/12 05:00 AM, Kirk-Cocaine wrote: Check out the ...rest keyword.
I learned something today!
but again: In my entire life the maximum number of parameters I have ever used in one function is five. And I know I could have cut that down to two or three.
Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P
At 5/22/12 05:00 AM, Kirk-Cocaine wrote: Check out the ...rest keyword.
Awesome thank you! Exactly what I'm looking for!
At 5/22/12 05:05 AM, egg82 wrote: I learned something today!
but again: In my entire life the maximum number of parameters I have ever used in one function is five. And I know I could have cut that down to two or three.
It's not that I need infinite parameters. It's just by having a function with that option I can call it with varying amount of parameters instead of the same number of parameters each time.
At 5/22/12 05:05 AM, egg82 wrote: but again: In my entire life the maximum number of parameters I have ever used in one function is five. And I know I could have cut that down to two or three.
Congratulations. Have you considered that the OP isn't you, is working on projects different than you, which therefore have different requirements?
Why even post if you're just going to provide superfluous anecdotes?
At 5/22/12 10:06 AM, Diki wrote:At 5/22/12 05:05 AM, egg82 wrote: but again: In my entire life the maximum number of parameters I have ever used in one function is five. And I know I could have cut that down to two or three.Congratulations. Have you considered that the OP isn't you, is working on projects different than you, which therefore have different requirements?
Why even post if you're just going to provide superfluous anecdotes?
well, I figured that much. I just never saw a need for that specific idea and wondered to what use it was being put.
Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P
At 5/22/12 10:14 AM, egg82 wrote: well, I figured that much. I just never saw a need for that specific idea and wondered to what use it was being put.
There are many useful, and practical, applications to functions not having a limited number of arguments.
The most obvious example, as the OP pointed out, is the trace function. There is also the Array class which has multiple methods which use ... args keyword.
Imagine you have a function that doesn't care how much data is passed to it. It takes all of it, and does something with it.
You could do something like this:
myFunction([1,2,3,4,5]);
Which, in the end, isn't much different than using the ... args keyword, as it using that creates an array anyways.
However, compare the following:
myFunctionA([[1,2,3], [4,5,6], [7,8,9]]);
myFunctionA([1,2,3], [4,5,6], [7,8,9]);
The second is easier to read, and if it's easier to read that is a good thing.
As well, when you use that keyword, your function definitions can become more legible. Consider the following:
function foo(name:String, data:Array=null);
function bar(name:String, ... args);
The first intuitively allows the second argument to be omitted, but it's not entirely clear if there could be a repercussion for said omission.
If no array is passed to the second argument then data will have to be NULL. If the constructor requires data to be non-NULL then a check for NULL is required:
function foo(name:String, data:Array=null)
{
__mSomeThing = {name:(data !== null ? data : new Array)};
}
However, if you use the ... args keyword, an array will always be created, even if no secondary, third, fourth, et cetera, parameters are passed. So, the code would instead look like this:
function foo(name:String, ... args)
{
__mSomeThing = {name:args};
}
The keyword is very much a case of "if you're not sure if you need it, then you probably don't". It's not something that you will frequently require, but that doesn't make it any less of a useful feature.
In summary: it's not about having the ability to pass a large number of parameters, it's about making your code more intuitive, legible, and less error-prone. Arguably it doesn't provide any additional functionality and is purely syntactical, not unlike comparing a while loop to a for loop.
At 5/22/12 10:35 AM, Diki wrote:At 5/22/12 10:14 AM, egg82 wrote: well, I figured that much. I just never saw a need for that specific idea and wondered to what use it was being put.There are many useful, and practical, applications to functions not having a limited number of arguments.
The most obvious example, as the OP pointed out, is the trace function. There is also the Array class which has multiple methods which use ... args keyword.
Imagine you have a function that doesn't care how much data is passed to it. It takes all of it, and does something with it.
You could do something like this:
myFunction([1,2,3,4,5]);
Which, in the end, isn't much different than using the ... args keyword, as it using that creates an array anyways.
However, compare the following:
myFunctionA([[1,2,3], [4,5,6], [7,8,9]]);
myFunctionA([1,2,3], [4,5,6], [7,8,9]);
The second is easier to read, and if it's easier to read that is a good thing.
As well, when you use that keyword, your function definitions can become more legible. Consider the following:
function foo(name:String, data:Array=null);
function bar(name:String, ... args);
The first intuitively allows the second argument to be omitted, but it's not entirely clear if there could be a repercussion for said omission.
If no array is passed to the second argument then data will have to be NULL. If the constructor requires data to be non-NULL then a check for NULL is required:
function foo(name:String, data:Array=null)
{
__mSomeThing = {name:(data !== null ? data : new Array)};
}
However, if you use the ... args keyword, an array will always be created, even if no secondary, third, fourth, et cetera, parameters are passed. So, the code would instead look like this:
function foo(name:String, ... args)
{
__mSomeThing = {name:args};
}
The keyword is very much a case of "if you're not sure if you need it, then you probably don't". It's not something that you will frequently require, but that doesn't make it any less of a useful feature.
In summary: it's not about having the ability to pass a large number of parameters, it's about making your code more intuitive, legible, and less error-prone. Arguably it doesn't provide any additional functionality and is purely syntactical, not unlike comparing a while loop to a for loop.
If I may:
First, I would like to point out that (in AS2 at least) the trace() function does not have an unlimited number of arguments. It mas one argument called message which is a string. You may concatenate or manipulate that string in whichever way, shape, and form you want, but in the end it only takes one argument.
side note: I just tried passing two arguments through - trace("test", "test2") - and received a compiler error in return.
other than that your argument look solid. Two questions, though:
One: Does args require an array? A string? An integer? Or can it be anything?
Two: How would you practically use the args in your code inside the function? I can't see
if(args1 != null and args1 != undefined and args1 != ""){
//do something
}
if(args2 != null and args2 != undefined and args2 != ""){
//do something
}
if(args3 != null and args3 != undefined and args3 != ""){
//do something
}
as being practical and/or justifiable of using the args keyword
Programming stuffs (tutorials and extras)
PM me (instead of MintPaw) if you're confuzzled.
thank Skaren for the sig :P
At 5/22/12 11:02 AM, egg82 wrote: First, I would like to point out that (in AS2 at least) the trace() function does not have an unlimited number of arguments.
I am aware; I wasn't referring to AS2. I was referring to AS3.
AS2 is almost a decade old, and vastly inferior to AS3; there isn't any reason to be using it.
At 5/22/12 11:02 AM, egg82 wrote: One: Does args require an array? A string? An integer? Or can it be anything?
You can pass anything. All of the parameters that are passes are populated inside of an array, so if you were to pass an array as an argument you would have a two-dimensional array.
At 5/22/12 11:02 AM, egg82 wrote: Two: How would you practically use the args in your code inside the function? I can't see
The same way you would use an array; that's all the keyword creates:
function foo(... args):void
{
var length:uint = args.length;
var first:* = args[0];
var second:* = args[1];
var third:* = args[2];
for each (var value:* in args) {
trace(val);
}
}
(Note: this is only an example; don't actually code like this)
At 5/22/12 06:15 AM, Venks wrote: It's not that I need infinite parameters. It's just by having a function with that option I can call it with varying amount of parameters instead of the same number of parameters each time.
maybe default parameters are also applicable.