Functions - Advanced Use
Setting parameters with default value
• Default value becomes optional, you can call the function without adding an
argument for that parameters. If you not pass an argument, the function will use it
with the default value .
• function sayHy(name:String = 'You') {
trace('Nice to meet '+ name);
}
sayHy('Marius'); // displays: Nice to meet Marius
sayHy(); // displays: Nice to meet You
Using the rest parameter (...)
• The "rest parameter" is a symbol (...) that represents a variable number of
arguments as an array followed by a name for that array(... array_name).
• // define a function with "rest parameters"
function testF2(... nums):void{
var latura:Number = 0;
for each (var num:Number in nums){
latura += num; // add the current value to "latura
}
testClip.width = latura;
testClip.height = latura;
}
testF2(8, 20, 40, 50); // calls the functions with multiple arguments
Assign function to a variable
• The variables declared outside a function (before the function definition) can be
used in the function code. The variables created within a function can only be used
inside the code of that function. These variables are called "local variables", and
they not exists outside the function.
// define a function with 3 parameters in a variable
var vrF:Function = function (obj:MovieClip, sx:Number, sy:Number):*
{
// "obj" must be the instance of a MivieClip
// define "scaleX" and "scaleY" properties for "obj
obj.scaleX = sx;
obj.scaleY = sy;
}
// call the function, passing the 'testClip' instance for the "obj" parameter
vrF(testClip, 2.5, 1.4);
Recursive Functions
• A recursive function is a function that calls itself.
To avoid infinite recursion (when a function never stops calling itself), use an "if()"
condition to control when a recursive function calls itself.
One classic use of the recursive functions is to calculate the mathematical factorial
of a number, which is the product of all positive integers less than or equal to the
number (1*2*3*4*...*n)
• // define a recursive function
function factorial(n:uint):uint
{
// if n>0, multiply 'n' auto-calling function
if(n > 0) {
return (n * factorial(n-1));
}
else { return 1; } // return 1 when n>0 is false
}
// store in a variable the value returned by factorial(8)
• var fact:uint = factorial(8);
• trace(fact); // Output: 40320

Chapter iii(advance function)

  • 1.
  • 2.
    Setting parameters withdefault value • Default value becomes optional, you can call the function without adding an argument for that parameters. If you not pass an argument, the function will use it with the default value . • function sayHy(name:String = 'You') { trace('Nice to meet '+ name); } sayHy('Marius'); // displays: Nice to meet Marius sayHy(); // displays: Nice to meet You
  • 3.
    Using the restparameter (...) • The "rest parameter" is a symbol (...) that represents a variable number of arguments as an array followed by a name for that array(... array_name). • // define a function with "rest parameters" function testF2(... nums):void{ var latura:Number = 0; for each (var num:Number in nums){ latura += num; // add the current value to "latura } testClip.width = latura; testClip.height = latura; } testF2(8, 20, 40, 50); // calls the functions with multiple arguments
  • 4.
    Assign function toa variable • The variables declared outside a function (before the function definition) can be used in the function code. The variables created within a function can only be used inside the code of that function. These variables are called "local variables", and they not exists outside the function. // define a function with 3 parameters in a variable var vrF:Function = function (obj:MovieClip, sx:Number, sy:Number):* { // "obj" must be the instance of a MivieClip // define "scaleX" and "scaleY" properties for "obj obj.scaleX = sx; obj.scaleY = sy; } // call the function, passing the 'testClip' instance for the "obj" parameter vrF(testClip, 2.5, 1.4);
  • 5.
    Recursive Functions • Arecursive function is a function that calls itself. To avoid infinite recursion (when a function never stops calling itself), use an "if()" condition to control when a recursive function calls itself. One classic use of the recursive functions is to calculate the mathematical factorial of a number, which is the product of all positive integers less than or equal to the number (1*2*3*4*...*n) • // define a recursive function function factorial(n:uint):uint { // if n>0, multiply 'n' auto-calling function if(n > 0) { return (n * factorial(n-1)); } else { return 1; } // return 1 when n>0 is false } // store in a variable the value returned by factorial(8) • var fact:uint = factorial(8); • trace(fact); // Output: 40320