First time here? First time here? Newsletter Signup Newsletter
Login | Register | 651326 Members and growing!
Search tutorials
Author: Andy Armistead (with a little help from FlashGuru)

A function for Going Shopping

Take going shopping at the supermarket. It’s something we do over and over again. It’s basically the same tasks every time. So if we could write functions in real life, it would be a good candidate for one. To write such a function, I first need to think about the individual tasks that are involved in ‘shopping at the supermarket’. The list of tasks might turn out looking something like this:

go to the supermarket 
buy the things on my list 
pay for the food 
come home 
put food away 

This could be made into a function. I define the function by doing something like this:

function goShopping () { 
go to the supermarket;
buy the things on my list;
pay for the food; 
come home; 
put food away;
} 
Voila! One definition of shopping.

I've given the function a name ‘goShopping’. This is so that when we actually use the function later, flash knows which actions it needs to do. All of the tasks that the function ‘goShopping’ includes are put inside the curly brackets. You can think of them a little like:

{ means “tasks start here” } means “tasks stop here”

Parameters change, tasks don't

You'll notice that in order to do my shopping I need a shopping list and that might change each time I go shopping. I need to write a new list each time. The tasks inside the function’s curly brackets will be the same every time the function is done.

So instead of putting the list into the function where it would be the same every time I went shopping, I need to give the function a different list each time I do it. I send the shopping list in using a 'parameter'. Like this:

function goShopping (shoppingList) { 
go to the supermarket;
buy the things on my: shoppingList; 
pay for the food;
come home;
put food away;
} 

The parameter is just a bit of information that the function needs to have in order to do its tasks properly. When we are defining the function, we need to tell it to expect a shopping list, even though we can’t know yet exactly what the shopping list will look like.

A parameter is a variable that can change every time the function is done.

Getting a return on your effort

OK... but the whole point of going shopping was to get food. What happened to that? Well… I want to get some food out of the function. A bit of the function has already bought the food (according to my list) so I'm most of the way there. But I need to tell my function to give it to me. I do this using 'return'

function goShopping (shoppingList) { 
go to the supermarket 
buy the things on my: shoppingList 
pay for the: foodBought 
come home 

return: foodBought 
} 

“return” is a special word inside a function. It tells the function what it is supposed to spit out at the end. Sometimes a function won’t need to return anything, but when it does, this is how we tell it what it is that we want back.

Doing the deed

To actually go shopping I need to 'do' the function. This is sometimes called 'calling the function' or 'invoking the function'. It all boils down to the same thing though, we want the tasks in the function done! In order to do that we need to tell flash to go do the shopping and give it a shopping list. So now every time I want to go shopping, all I have to do is say, for example:

todaysList = “milk,sugar,baked beans,loadsa chocolate”
goShopping (todaysList); 
and the shopping will get done properly using todaysList. But where does the food go? Remember, we told the function to return the food to us and rather than have it just all over the floor, we should put it somewhere, so I put it into something that can hold different stuff: a variable... (well I put food in the cupboard actually)... so a variable called ‘cupboard’!
todaysList = “milk,sugar,baked beans, loadsa chocolate”
cupboard = goShopping(todaysList);
So this will send someone shopping (hopefully not me) with today’s shoppingList and will fill the cupboard with the food bought while shopping.

If my function doesn’t return anything (and not all of them do), then I don’t need somewhere to put it, but because this one returns the food, I’ll need to put it somewhere.

A variable can be used to store the results.

Where does what go and when?

So, now with flash, it's usually a good idea to get the function definitions, (where you tell flash which tasks it should do) right at the beginning in Frame 1 of scene1 of the movie. Why? because flash must read its instructions before it can do the job (makes sense!). If we give flash all of its instructions at the very beginning, then whenever we tell it what to do, we can be sure it has already read the instructions.

So in Frame 1 of the main timeline we put the function definition:

function goShopping (shoppingList) { 
go to the supermarket 
buy the things on my: shoppingList 
pay for the: foodBought 
come home 

return: foodBought 
}
For the rest of this movie, we'll be able to tell Flash to go shopping whenever we want using:
todaysList = “milk,sugar,baked beans, loadsa chocolate”
cupboard = goShopping(todaysList);
Or if we want to get shopping on thursday too, we might do the following somewhere else
ThursdaysList = “chocolate, beans, spam, beans, chocolate, spam”
thursdayCupboard = goShopping (thursdaysList);
Easy isn't it?

» 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!

Comments

  • 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.