First time here? First time here? Newsletter Signup Newsletter
Search tutorials
Author: Andy Armistead (with a little help from FlashGuru)

Our first Custom Function

Now, 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 tasks

I 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 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. That’s 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, 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 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, 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!

» Level Basic

Added: 2001-05-16
Rating: 8 Votes: 185
(10 being the highest)
» Author
No details available.
» Download
Download the files used in this tutorial.
» Forums
More help? Search our boards for quick answers!


  • There are no comments yet. Be the first to comment!

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.
Featured Flash FLA
» Author: Inocreato
» Title: RaiseTheBlocks
» Description: Raise all the blocks to win the game
Featured Sound Loops
Image for equalize

Audio Player

» Author: TomCat Carty
» Title: The Wood
» Description: Just a little game ending or it can maybe be looped. Recorders with music box and percussion to give the feel of well, I don't know, the woods? Free to use, just credit me. thank you
Latest Font
» Author: Fábio FAFERS
» Description: I created this font for free use. Everyone can apply it in personal or business texts. Its free, but I want to be communicated in case of business use. Donations are accepted to keep the project of free fonts alive! Thank you all
Featured Sound Fx
Image for equalize

Audio Player

» Author: Davisigner
» Description: Hmm... what to say about this one? It's reminiscent of the closing notes of the opening music from the Three Stooges done in a church organ style with a closing cymbal crash. I'll give this one away gratis, but feel free to check out my free loops and potential upcoming license-mandated ones over in the respective part of Flashkit.