A function is a set of instructions (a block of code) that are executed only when you access the functions.
Functions allow you to organize your code into independent pieces that can accomplish different tasks.
Functions can be used more than once within a script to perform their task. Rather than rewriting the entire block of code, you can simply call the function again.
function functionName(param1, param2, ...) { // code to execute (the function body) }- The complete form (with DataType)
function functionName(param1:DataType, param2:DataType, ...):fDataType { // code to execute (the function body) }- functionName - is the name of the function. It is used when you want to run (access) the function.
function getMsg(msg:String) { trace(msg); }- This function has a parameter, msg, which is defined to contain String data type. When you'll call this function, the code within curly brackets is executed (trace(msg)).
functionName()- If the function has parameters, use the following syntax:
functionName(arg1, arg2, ...)- arg1, arg2, ... - are the arguments (values) passed to the function, in order, to function's parameters. The number and type of values passed in must match the function definition.
// define the function function getMsg(msg:String) { trace(msg); } var site:String = 'coursesweb.net'; // define a String variable // calling the function two times getMsg('Welcome to'); getMsg(site);When the getMsg() function is called, the value of the argument is passed to the function.
function functionName(param1:DataType, param2:DataType, ...):fDataType { // code to execute return someValue; }- someValue can be a primitive (a string, a number, a boolean), a variable, or a expresion.
// define a function with two numeric parameters, and returns the value of a variable function addAB(a:Number, b:int) { // sets a variable to store the result of a+b var c:Number = a+b; return c; // return the value of 'c' } // calling the addAB() and store the result in a variable var adds = addAB(7.5, 8); trace(adds); // output the value of "adds": 15.5- The "adds" variable stores the value returned by the function. The trace function will output this value: 15.5.
You can use the return statement in any function, with, or without parameters.
// define a function with two numeric parameters, and returns a int data type function addAB(a:Number, b:int):int { // sets a variable to store the result of a+b var c:Number = a+b; return c; // return the value of 'c' } // calling the addAB() and store the result in a variable var adds = addAB(7.5, 8); trace(adds); // output the value of "adds": 15- ":int" after the function's parentheses force the function to return only integer values.
function functionName(param1, param2, ...):void { // code to execute, but without return }
<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