# Create a Function

In Flash 5 can you create your own function in in the first frame and the call to it through out the scene? And if you can can some one post an example of what this would look like.

Functions are fun, and great tools.

Lets see if I can come up with something cool that will help you understand a lot of different aspects of functions.

Thanks man that would be great.

to make a function you must construct it. Wherever you create it, you will have to call it using that location. This is why we often place them on the main timeline first frame. In ensures that later when we need them, they are easy to find.

function myFirstFunction(a,b){
average=Math.round((a+b)/2);
return average;
}

What this does is create a function called “myFristFunction” which accepts 2 arguements. a and b. Arguements are things that you can send into the function to be manipulated. Even a movie clip name can be sent in.

If I had a button set up with the following code.

on(release){
_root.myFirstFunction(5,8);
trace(average);
}

we could test the movie (and if I haven’t made a mistake) when we press the button an output window will pop open with the number 7.

the button sends 5 and 8 to the function they are added, divided by 2 and then subjected to the Math.round() method which rounds off the number. The variable called average is created, and has the value of that mathematical formula. Return is something that exists in every function wether you place it at the end or not… but in this case we want the function to send the variable average back to whatever called this function. (the button) so we use “return average” to do that.

the trace action in the button works in test mode to pop up a trace window with the value of the returned variable.

all in all… that is the most basic of uses for a function. Often I use functions to create movie clips or populate arrays.

You can also create your own prototype methods. Kinda fun You can check the AS tricks section for explanations.

pom :asian:

Thanks alot you guys. See i’m making a fuction with a bunch of If statments so I don’t have to have the same if satments repeated in every button. I can just call the function in each button and it will check a variable through the if statments and then return what frame of animation it should preforme. When I get it all done I’ll be sure to post the results. Thanks Again:)