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.

There are two general kind of functions:

Creating a Function

Functions are defined (created) using a special predefined keyword, function, followed by the function name, a set of parentheses and the function body (within curly brackets).
There are two syntaxes to define a function, with, or without specifying the DataType.

- The simple form (without DataType)
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.
- param1, param2, ... - is an optional list of parameters.
- DataType - specifies a type of data that the associate element can contains.
- fDataType - (after the parentheses) sets the type of data that the function can return. If the function doesn't return a value (not use the "return" instruction), you can use void for fDataType.

Example of a simple function:
function getMsg(msg:String) {
- 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)).

Calling a function

To execute the code of a function you must call (or invoke) it, by using the function's name followed by a pair of parentheses ().
- If the function is defined without parameters, use this syntas:
- 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.
You can call a function multiple times. The code of the function is executed in the location where we invoke it.

In the next example we create a function with a String parameter, and then we call it two times (using a direct value for the argument, and a value contained in a variable):
// define the function
function getMsg(msg:String) {

var site:String = 'coursesweb.net';       // define a String variable

// calling the function two times
getMsg('Welcome to');
When the getMsg() function is called, the value of the argument is passed to the function.
The function stores this value in the "msg" parameters, and use it in its code.
- If you add this function in the Action panel of a new Flash document, and press "Ctrl+Enter", the Output panel will show the following result:
Welcome to

Using the return

A return statement is used to specify a value that a function returns when is called.
The "return" keyword must be followed by the value that you wish to return.
The syntax for creating a function that returns values is:
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.
- The "fDataType" is optional. If you add it, the data type of "someValue" must match the "fDataType".
You place the return statement as the last line of the function (before the closing curly bracket), becouse when a return statement is executed in a function, the function ends and the rest of the code is not processed.

The value returned by a function can be assigned to a variable, like in the following example:
// 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.

Defining a return type

When you define a data type for a parameter with the colon (:) after the parameter name, it indicates what kind of information should be stored in the parameter.
When you add a DataType after the parentheses of a function, it specifies wat kind of data the function can return.
The Data Type can be any kind of data used for variables (String, Number, int, ...).
If you are unsure what type of data the function will return, you can use the wildcard data type (*), which represents untyped data. This will allow the function to return any type of data.

In the next example we use the same function defined in the example above, but this time we set a data type for the value that the function will return.
// 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.
- 7.5+8 = 15.5, but the function returns only the integer part (15).
So, if you specify or not a return DataType can determine the result of the function.

• Functions which not have a return statement can use a data type of void.
function functionName(param1, param2, ...):void
  // code to execute, but without return

Daily Test with Code Example

Which meta tag provides a short description of the page?
<meta content="..."> <meta description="..."> <meta http-equiv="...">
<meta name="description" content="70-160 characters that describes the content of the page" />
Which CSS property is used to stop the wrapping effect of the "float"?
clear text-align position
#some_id {
  clear: both;
Click on the method which gets an array with all the elements in the document that have a specified tag name.
getElementsByName() getElementById() getElementsByTagName()
var divs = document.getElementsByTagName("div");
var nr_divs = divs.length;
Indicate the PHP function which returns the number of elements in array.
is_[) count() strlen()
$arr =[7, 8, "abc", 10);
$nri = count($arr);
echo $nri;        // 4
Creating Functions in ActionScript

Last accessed pages

  1. Wake Up The Box 5 (147)
  2. Logic Perspicacity Games (8182)
  3. Desert Car (670)
  4. Linebacker Alley (1502)
  5. Puzzle Games (13306)

Popular pages this month

  1. Qwop (3504)
  2. Flash Games - Free online Games (1602)
  3. Get Attribute (ID, Class, Name, Title, Src) with jQuery (1543)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (1431)
  5. Drag Racer V3 (1420)