Our first Custom Function
Now, we dont really expect Flash to go shopping for us (though in the wonderful world of online shopping even this might be possible). Lets look at a real example of a useful function that we could regularly use in Flash.
We are going to write a function that will work out percentages for us. So, the way we usually say this is something like:
10 out of twenty is fifty percent.
Define the tasks
I havent done maths in many a year, but I do remember that we work out a percentage by doing:
(number/total)*100 = percentage(because in flash the / means divide by and the * means multiply by).
That is the task that we want to do, so in the same way as we did for shopping we would define the tasks as:
function percent() { percentage = (number/total)*100; }Weve given the function the name percent and told it what tasks it needs to do. Easy!
Work out the parameters
In order to do the tasks in the percent() function, it needs to know what value number has and what value total has, then it can do the rest. So we tell it to expect these values by specifying them as parameters:
function percent (number,total) { percentage = (number/total)*100; }Notice, that because we need to give two different parameters, we can just put them inside the round brackets separated by a comma. Pretty straightforward eh? Because we are defining the function, we can put as many and as few parameters as we like. Thats one of the beauties of writing custom functions you get to decide everything.
Getting the return
The point of doing this function is that we want to get the percentage back from it. Otherwise, thered be little point in doing it. So we need to return the percentage.
function percent(number,total) { percentage = (number/total)*100; return percentage; }Nothing could be easier.
Using the function
Remember from our shopping example, we needed something to put the food in, so we used a variable cupboard that would hold the food that the function returned. Well here, were returning the percentage so when we call the function we need to give it somewhere to put the returned value. Were going to use a variable called pcent. You could call the variable anything you like, the function wont be picky; itll put the returned value into whatever you tell it to.
So for our example, when we call the function we might do something like:
pcent = percent(10,20);this tells flash to do the tasks inside the function called percent(). It tells it that the value of number is 10 and that the value of total is twenty. Flash then goes off and works it all out for you and puts the result into the variable called pcent. So in this case pcent will be set to 50.
One thing to keep in mind here: The order of the parameters is important. If I do:
pcent = percent(20,10);then Flash will do:
percentage = (20/10)*100; return percentage;which would give us a completely different result. In this case pcent would equal 200.
» Level Basic 
Added: 20010516 Rating: 8 Votes: 185 
» Author 
No details available. 
» Download 
Download the files used in this tutorial. 
Download (0 kb) 
» Forums 
More help? Search our boards for quick answers! 

You must have javascript enabled in order to post comments.
Comments
There are no comments yet. Be the first to comment!