;

PDA

Click to See Complete Forum and Search --> : Optional Parameters


leifi
08-05-2007, 10:25 AM
Hi all,

I've got a simple question:
is it possible in 3DFA to give your functions optional parameters?


// c shall be the optional parameter

function myFunction(a,b,c) {
trace (a,b);
trace (a,b,c);
}

myFunction(1,2,3); // everthing is ok...
myFunction(1,2); // 3DFA shows an error


Error:
function myFunction(a,b,c) was given 2 parameters when 3 were expected on line 9 myFunction(1,2)


Anyone knows how to solve this?

Thanks,
leifi

kusco
08-06-2007, 02:51 AM
.. correction added ..

Finjogi
08-06-2007, 05:22 AM
It is bit mixed bag.. if you supply only 2 params then you have to check if 3.rd one is null, if you have to check it then it is no pain to supply 3.rd parameter as null :)

However, function overloading could be more useful, you could define 2 same-name functions to work with 2 or 3 params.... but then it is only visual cleaniness as you define myFunc(a,b) and myFunc(a,b,c)... so why not just do it old fashion way myFunc1 and myFunc2 :)

At the moment 3dfa does not accept multiple same name functions so... there is no much to do for now.

edit: I was slow :)

kusco
08-06-2007, 10:35 AM
The next release of 3DFA, due later this week, allows the use of optional parameters for functions - for both Flash 8 and 9 export.

You can define a function ...

function my_function (a, b=42, c="hello", d=true)
{

}

... and you can specify between one and four parameters when calling the function.

leifi
08-06-2007, 11:15 AM
Cool! Thank you for this fast response!
Maybe I have some more ideas for 3DFA...