Functions

I l @ ve RuBoard

Until now, we've placed all our code in short scripts attached to the first frame of the movie. This works well for short, simple programs, but these scripts can get very long if you try to make the program complex.

Functions allow you to organize and reuse your code. You place functions in the timeline just as we have been doing. Here is a simple function:

 function myFunction(num) {     var newNum = num + 3;     return newNum; } 

A function starts with the keyword function followed by the function name . Function names can be anything you want, just like variable names . But they should usually be something that relates to what the function does.

After the function name comes a left parenthesis. Then follows a list of parameters. A parameter is a variable that is defined when the function is called. Think of it as the input to a function. In this case, you are going to give the function a number to do something with.

You can have one, many, or no parameters. Either way, you close off the parameters section with a right parenthesis and then use an open bracket to start the function.

All the lines between the open and close brackets are the instructions inside the function. In this case, a new local variable is created, called newNum . The value of newNum is set to whatever num is, plus 3. So if you pass a 7 in to the function as num , newNum is now 10.

The return command is a special command used only inside functions. It completes the function and sets a value as the result of the function. In this case, newNum is the result of the function.

To use this function, call it like it was a standard ActionScript function or command, such as trace . Here is an example:

 var a = myFunction(7); 

This line of code creates a new local variable called a . It places in it the results of myFunction(7) . To determine this value, myFunction is called with the number 7 as its only parameter.

When the function starts, it creates a local variable called num and places 7 inside it. It then runs the code inside, which ends with the return command sending the value 10 back to the thing that originally called the function. In this case, a gets set to 10.

A great thing about functions is that you can reuse them. Here are three lines of code that reuse the function to produce three different results:

 trace(myFunction(7)); trace(myFunction(13)); trace(myFunction(2)); 

When you run this code, along with the function included before it, you will get the results 10, 16, and 5. Another advantage to using functions is that you can make one change in the function, and it will affect all the commands that use that function. For instance, if you change the + 3 in the function to + 4 , the results of the preceding three lines become 11, 17, and 6.

I l @ ve RuBoard


Sams Teach Yourself Flash MX ActionScript in 24 Hours
Sams Teach Yourself Flash MX ActionScript in 24 Hours
ISBN: 0672323850
EAN: 2147483647
Year: 2002
Pages: 272

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