After the discussion about conditionals and looping, let’s turn to the new topic, function. Before our discussion, let’s do some practice first.
Introduction
Now, your friend, a teacher in a school, needs your help. He wants to print the students’ scores of mathematics, history and music in his class. And, he knows that you’re a genius developer, so he asks for your help.
Suppose the poor teacher stores the scores in the corresponding array like below:
- var math:Array = [70, 80, 65, 90, 85, 100];
- var history:Array = [90, 89, 92, 82, 78, 95];
- var music:Array = [72, 75, 89, 92, 65, 79];
So, your task is to print the values stored in the arrays.
Eh, it seems that we need to iterate all the values stored in the arrays. Remember the “for statement”? Maybe we can use the “for statement” or other looping statement for the iteration. And the version of “for statement” implementation is like below:
- var math:Array = [70, 80, 65, 90, 85, 100];
- var history:Array = [90, 89, 92, 82, 78, 95];
- var music:Array = [72, 75, 89, 92, 65, 79];
- trace(”============Mathematics==========”);
- for(var i:int = 0; i < math.length; i++) {
- trace(math[i]);
- }
- trace(”=============end===============\n”);
- trace(”============History==========”);
- for(i = 0; i < history.length; i++) {
- trace(history[i]);
- }
- trace(”=============end===============\n”);
- trace(”============Music==========”);
- for(i = 0; i < music.length; i++) {
- trace(music[i]);
- }
- trace(”=============end===============\n”);
Run the code you can see the result in the output panel.
OK, that’s it, genius developer, your task is finished. Your friend very appreciates your excellent work. But, he feels that the output format maybe need some change. He wants to know the index of each value. That means he want to change the current output into:
Aha, a piece of cake, you think. You just need change the output statements in the iteration and then you can get the result.
Eh, here you just need to modify three statements, but what if the teacher friend wants to print more kinds of scores or change the output format to be another one.
OK, let’s go back to the code we write first. And the form is like:
- trace(“subject”);
- for(var i:int = 0; i < subjectArray.length; i++) {
- trace(i + “:” + subjectArray [i]);
- }
- trace(”end\n”);
The similar code appears in the code we wrote three times.
Three times that means redundancy and hard to maintain here.
Look at the three blocks carefully; you can see the difference between each block is just the array name.
If we use a temporary Array type variable,
- var subjectArray:Array;
Then, we can rewrite the code into:
- trace(”============Mathematics==========”);
- subjectArray = math;
- for( i = 0; i < subjectArray.length; i++) {
- trace(i + “:” + subjectArray[i]);
- }
- trace(”=============end===============\n”);
- trace(”============History==========”);
- subjectArray = history;
- for( i = 0; i < subjectArray.length; i++) {
- trace(i + “:” + subjectArray[i]);
- }
- trace(”=============end===============\n”);
- trace(”============Music==========”);
- subjectArray = music;
- for( i = 0; i < subjectArray.length; i++) {
- trace(i + “:” + subjectArray[i]);
- }
- trace(”=============end===============\n”);
Now, the only difference between each block is the assignment statement. Further more, if we can use something short instead of the blocks, we can get more concise code.
Maybe like:
- trace(”============Mathematics==========”);
- subjectArray = math;
- printScore();
- trace(”============History==========”);
- subjectArray = history;
- printScore();
- trace(”============Music==========”);
- subjectArray = music;
- printScore();
Here, “printScore()” is stand for:
- for( i = 0; i < subjectArray.length; i++) {
- trace(i + “:” + subjectArray[i]);
- }
- trace(”=============end===============\n”);
If we can rewrite the code like this, it will be very convenient. Maybe we can declare the printScroe equals to the block of code in somewhere else
Now, If the teacher friend want to print more kinds, you just need to paste the printScore() a few more times. If the teacher wants to change the output format, you just need to change one trace statement.
Note: this is not an actual solution; don’t try to run the code. The actual solution will be given in the next section.

April 8th, 2009
Ntt.cc 








Posted in
Tags: 
RSS Feed
Email Feed
thanks. it looks great!