function()
Top  Previous  Next


SWiSH Player Support
SWF5 or later - Supported Internally

Syntax
function name ([arg [ : type ] [, arg [ : type ]]...]) [ : type ] {
statements;
}

-OR-

function ([arg [ : type ] [, arg [ : type ]]...]) [ : type ] {
statements;
}

Arguments
name: Name of the function being defined.
arg: Optional function argument names
type: Optional type of the argument and return value
statements: The script to execute when the function is called

Returns
The first syntax does not return a value.
The second returns a function object.

Description
The first syntax defines a named function. You can call the function with a function call (add link to call function here).

The second syntax is an expression that defines an unnamed function object and returns a reference to it. You can assign that function object to a variable and call the function using that variables. This syntax is often used to set up event handlers in SWF6+.

Both syntaxes support optional argument and return value types. These types may be used by the SWiSHscript compiler to test whether the values passed to and returned by a function are of the specified types. Types can include Number, Boolean or String.

When the function is called, the script is executed. The function argument names become variables whose values are the values passed to the function. The return value of the called function will be the value used in the return statement.

Sample

Example 1: First Syntax

onLoad () {
    a = 0;
    r = 3;
    a = _root.area(r);
    trace(a);
}


function area(p) {
    return Math.PI * p * p;
}


// On completion, 'a' contains the area for a radius of 3 (pi * r^2), 28.2743346691132


Example 2: Second Syntax

onLoad() {
    f = function (x) { return x*x; }

    y = f(10
); // calls f with 10, returns 100
    trace(y);
}

Example 3: Functions in Dynamic Event Handlers (SWF6+)

onFrame (10{
    MySprite.onRollOver = function () {
      MyTextField.text = "Roll Me Over!";
    }
;
}
onFrame (20
{
    MySprite.onRollOver = function () {
      MyTextField.text = "Roll Me Over Again";
    }
;
}

// Note: The above example only works in SWF6+ and is not supported by the internal SWiSHmax player (Test in Browser/Player from the File Menu)

See Also
call function