Using variables in functions

Local variables are valuable tools for organizing code and making it easy to understand. When a function uses local variables, it can hide its variables from all other scripts in the SWF file; local variables are invoked in the scope of the body of the function and cease to exist when the function exits. Flash also treats any parameters passed to a function as local variables.

NOTE

You can also use regular variables in a function. However, if you modify regular variables, it is good practice to use script comments to document these modifications.

To use variables in functions:

  1. Create a new Flash document and save it as flashvariables.fla.
  2. Add the following ActionScript to Frame 1 of the main Timeline:
    var myName:String = "Ester";
    var myAge:String = "65";
    var myFavSoftware:String = "Flash";
    function traceMe(yourFavSoftware:String, yourName:String, yourAge:String) {
        trace("I'm " + yourName + ", I like " + yourFavSoftware + ", and I'm " + yourAge + ".");
    }
    traceMe(myFavSoftware, myName, myAge);
    
  3. Select Control > Test Movie to test the Flash document.

For more information on passing parameters, see Passing parameters to a function. For more information on variables and data, see Data and Data Types