NAVIGATION:

 

SUPPORTERS:

FlashComponents
  Flash Templates
  Flash Gallery
  Flash Slideshow
  Flash Menu
  Flash Design
  Flash Video
  User Interface

 

FOLLOW:

RSS it down! Twitter it up! Facebook it like a fiend!

 

 

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:

additionator = function(add_me) {
total = 10 + add_me;
trace(total);
}
additionator(4);

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:

additionator = function (answer) {
total = Math.round((Math.random()*10)+1);
return total;
};
final_answer = additionator(answer);
trace(final_answer);

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
};
answer = name(data)

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:

Download Source

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:

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

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

voluminatorizer = function (answer) {

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() {
text_answer = voluminatorizer(answer);
};

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.

I hope the information helped. If you have any questions or comments, please don't hesitate to post them on the kirupa.com Forums. Just post your question and I, or our friendly forum helpers, will help answer it.

The following is a list of related tutorial and help resources that you may find useful:

How to use the Forums
New, Upcoming, and In-Progress Tutorials
How to Help out kirupa.com
Writing Tutorials
 

Cheers!

Kirupa Chinnathambi
facebook | twitter

 

1 | 2

SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple. flash components
Creative web apps. Make your own free flash banners and photo slideshows.
Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.

Flash Transition Effects

Flash Effect Tutorials

Digicrafts Components Flash effects. Art without coding.
Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com Flipping Book - page flip flash component.
Flash-Gallery.com - Get your flash photo gallery (flash component or swf gallery Citrus Engine: Flash platformer and sidescrolling game engine
Learn how to advertise on kirupa.com

cdn
content delivery network (cdn)