## Question of the Week

Functions
by kirupa | 27 December 2005

In the previous page, we learned a lot about the basics about functions. In this page, we will explore some variations to functions by introducing arguments and the art of passing data! We'll wrap this tutorial up with an explanation of how my animation on the previous page works.

Passing Data to a Function
A function does not have to be static and display some event independent of the function that is calling it. You actually have the ability to modify your function to accept data while it is being called.

For an example, copy and paste the following lines of code into a frame in your Flash movie, and press Ctrl + Enter:

trace(total);
}

You have a function like you would normally, but this time, your call statement is passing data to your function. Here is the basic structure of one of those functions:

name = function (data) {
actions involving data
};
name(data_to_pass)

Passing data to a function is very useful, and this helps make your functions more interactive with the rest of your movie.

Retrieving Data from a Function
While you learned how to send data to a function in the above section, this section will explain how to retrieve data from a function. The coding is not radically different, but there are some peculiarities. First, though, here is an example you can try in your own animation:

total = Math.round((Math.random()*10)+1);
};

If you run the above code in a Flash movie using Ctrl + Enter, you will see that the data from your function is displayed in an Output window. This seems very similar to our first example though, right? Not really!

In our first case, the trace action was contained inside our function. In our current example, the trace action is outside of the function. The data made its way outside of the function!

The way it works is that the variable total, is sent back to the the call function using the return method. The basic structure is as follows for a function of this type:

name = function (data) {
actions

return something
};

Take care to note the similarities and differences in the variable names between this scenario and the second scenario where the data is sent to a function. Ensuring that the variables are named appropriately for the function arguments and their call function will ensure that you will pass or receive data without receiving any errors.

The Example Animation Explained
Not to leave you without explaining how the example animation works, download and open the source file for the example animation:

Once you open that animation, take a few seconds to browse through the various names of the movie clips, text fields, and buttons. Once you have done that, view the Actions contained in the first frame.

Here is the code that I used:

l = lnt;
w = wt;
h = ht;
the_volume = l*w*h;
return the_volume;
};
submit.onRelease = function() {
};

The above code is very similar to the code used earlier. Here is a brief explanation as to each section of code accomplishes:

This line defines the function. The name of the function is voluminatorizer, and the function passes data via the variable answer to the call function.

l = lnt;
w = wt;
h = ht;

These three lines retrieve data from the three text fields named lnt, wt, and ht and store them in the variables, l, w, and h.

the_volume = l*w*h;
return the_volume;

The first line does the actual volume calculation and assigns the answer to the variable the_volume.

The second line is the return line. This line sends the data back to the function that calls it.

submit.onRelease = function() {
};

The first line is an event handler for a button instance named 'submit'. When the mouse is released, the variable text_answer receives the data from the voluminatorizer function via use of the answer variable from the argument.

Just a final word before we wrap up. If you have a question and/or want to be part of a friendly, collaborative community of over 220k other developers like yourself, post on the forums for a quick response!

 1 | 2