Creating Functions; Defining A Function - MACROMEDIA FLASH MX 2004-USING ACTIONSCRIPT IN FLASH Use Manual

Using actionscript in flash
Hide thumbs Also See for FLASH MX 2004-USING ACTIONSCRIPT IN FLASH:
Table of Contents

Advertisement

Creating functions

You can define functions to execute a series of statements on passed values. Your functions can
also return values. After a function is defined, it can be called from any Timeline, including the
Timeline of a loaded SWF file.
A well-written function can be thought of as a "black box." If it has carefully placed comments
about its input, output, and purpose, a user of the function does not need to understand exactly
how the function works internally.
For more information, see the following topics:

Defining a function

As with variables, functions are attached to the Timeline of the movie clip that defines them, and
you must use a target path to call them. As with variables, you can use the
declare a global function that is available to all Timelines and scopes without using a target path.
To define a global function, precede the function name with the identifier
the following example:
_global.myFunction = function (x:Number):Number {
return (x*2)+3;
}
To define a Timeline function, use the
any parameters to be passed to the function, and the ActionScript statements that indicate what
the function does.
The following example is a function named
function areaOfCircle(radius:Number):Number {
return Math.PI * radius * radius;
}
You can also define a function by creating a function literal-an unnamed function that is
declared in an expression instead of in a statement. You can use a function literal to define a
function, return its value, and assign it to a variable in one expression, as shown in the
following example:
area = (function() {return Math.PI * radius *radius;})(5);
When a function is redefined, the new definition replaces the old definition.
For information on strictly typing function return types and parameters, see
on page
41.
statement followed by the name of the function,
function
with the parameter
areaOfCircle
identifier to
_global
, as shown in
_global
:
radius
"Strict data typing"
61

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLASH MX 2004-USING ACTIONSCRIPT IN FLASH and is the answer not in the manual?

This manual is also suitable for:

Flash mx 2004 - actionscript

Table of Contents