In this lesson there are presented more advanced things about using functions.
The exemples in this tutorial use an Instance of a Movie Clip symbol created on the Stage (in Flash).
So, before to start testing the exemples below, open a new Flash document, select "Rectangle tool" and draw a rectangle on the Stage, then convert it in Movie Clip symbol (from Modify -> Convert to Symbol, and for Type select "Movie Clip").
Make sure the object is selected, open the "Properties panel" and add a name for this instance, testClip, like in the picture below.
function sayHy(name:String = 'You') { trace('Nice to meet '+ name); } sayHy('Marius'); // displays: Nice to meet Marius sayHy(); // displays: Nice to meet You- As you can see, the parameter with a 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.
All parameters that have a default value must be placed last in the order of arguments.
function setAccount(name:String, pass:String, email:String = null)
// define a function with 2 parameters, the second is default function testF(dy:Number, rotatie:int = 45):void { // sets "y" and "rotation" properties for "testClip" instance testClip.y = dy; testClip.rotation = rotatie; } // call the function, passing only the required argument (dy) testF(10);- "testClip.y" sets the Y distance, "rotation" is a property used to rotate the object.
// define a function with "rest parameters" function testF2(... nums):void { // declare a variable which will be used to store the sum of the values passed in arguments var latura:Number = 0; // define a "for each..in" to access each value stored in "nums" Array for each (var num:Number in nums) { latura += num; // add the current value to "latura } // uses the variable, "latura" to set the 'width' and 'height' for "testClip" testClip.width = latura; testClip.height = latura; } testF2(8, 20, 40, 50); // calls the functions with multiple arguments- All values passed to the "testF2()" function are stored in the "nums" Array.
The rest parameter can also be used with other required parameters. The required parameters will have their own names and will exist independent of the rest array. Any additional arguments passed after the required ones will be stored in the rest array.
function test(param1, ... rest_array)
var someVariabe:Function = function (param1, param2, ...) { // code to execute }- Once assigned, the function can then be invoked through that variable, as in:
someVariabe(arg1, arg2, ...)Because the function is assigned to a variable, it resides in memory as any normal variable. So, the value of this variable can be changed or deleted.
// 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);- the variable name (vrF) its the name used to call the function.
var ext_vr:int = 18; // variable created outside any function // define a function which use the "ext_vr" variable function oFunctie():Number { // create a local variable var in_vr:Number = 7; // add the value of the external variable "ext_vr" to the local variable in_vr += ext_vr; return in_vr; // return the values of "in_vr" } trace(oFunctie()); // displays in Output the value returned by the function (25) // Try to access the local variable "in_vr" outside the function (delete "//") // trace(in_vr);- Notice that the oFunctie() can use the value of the external variable "ext_vr". But if you try to access the local variable "in_vr" outside the function (deleting the "//" before trace(in_vr);), you'll get an error message.
// 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
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net