Search Tutorials
Our first Custom FunctionNow, we don’t really expect Flash to go shopping for us (though in the wonderful world of online shopping even this might be possible). Let’s 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 tasksI haven’t done maths in many a year, but I do remember that we work out a percentage by doing:
‘Number divided by the total possible. Then times it by 100 and that’s your percentage”
So this translates to:
(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;
}
We’ve given the function the name ‘percent’ and told it what tasks it needs to do. Easy!
Work out the parametersIn 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. That’s one of the beauties of writing custom functions… you get to decide everything.
Getting the returnThe point of doing this function is that we want to get the percentage back from it. Otherwise, there’d 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 functionRemember 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, we’re returning the percentage so when we call the function we need to give it somewhere to put the returned value. We’re going to use a variable called ‘pcent’. You could call the variable anything you like, the function won’t be picky; it’ll 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.
When you use a function, it is important that you give it the values it needs in the right order, otherwise the results you get will not be what you expected!
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|