Variables and Functions


When using variables in conjunction with functions, you need to follow several rules to avoid errors and increase consistency.

First, you should be cautious when using a variable name that's the same as the name of a parameter in the function when they both reside in the same script. This should be common sense (if for no other reason than for the sake of organized code), but let's say it happens anyway.

Create a variable and call it myVariable; then create a function and have it trace myVariable:

 var myVariable:String = "Flash"; //now create the function with no parameters function myFunction ():Void{     trace(myVariable); } //now run the function myFunction(); //output: Flash 

In this instance, the interpreter does not find myVariable anywhere in the function, so it begins to look outside the function, and it runs into the variable you created, which is named myVariable, and grabs the value of that variable.

Now let's see what happens when you use a parameter with the same name. Using the same code as before, add a parameter with the same name as the variable you created:

 var myVariable:String = "Flash"; //now create the function function myFunction (myVariable:String):Void{     trace(myVariable); } //now run the function myFunction("Unleashed"); //output: Unleashed 

This time, the interpreter found myVariable within the function itself, as a parameter name, and ignored the variable that was created before the function was created.

Finally, let's add a variable inside the function with the same name as the parameter and the variable you created before the function (again, using the same code as before):

 var myVariable:String = "Flash"; //Now create the function function myFunction (myVariable:String):Void{     var myVariable:String = "Professional";     trace(myVariable); } //Now run the function myFunction("Unleashed"); //output: Professional 

This time, the interpreter found the variable myVariable inside the function and didn't bother with the parameter or the variable you created before you created the function.

So now you know how the interpreter looks for variables: First, it looks in the function itself; then it looks at the parameters, and finally it looks outside the function.

The variables available within the function are not available outside the function. Let's take a look at an example:

 //First create the function function myFunction (myVariable:String):Void{     var myVariable:String = "Unleashed";     trace(myVariable); } //call the function myFunction(); //trace the variable trace(myVariable); //output: Unleashed //        undefined 

Notice how you cannot pull the variable name out of the function, even after the function has been invoked. This is important because that means that variables created inside of functions have a local scope to that function. After the function is run, the variable is no longer in existence, and you regain some (a small amount) of virtual memory back on a user's computer.

The next section will take functions a step further. As far as parameters are concerned, all we have covered involves using them to pass information to the script of a function. Now you'll see how to use them as objects.




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net