Beginning ActionScript 3.0 – Function First Sight

 

In last section, we use the printScore() stands for a block of code, in order to get rid of the redundancy and make the code more easier to maintain.

 

 

Actual Solution

Now, it’s time for the actual solution, or you may kill me

In ActionScript, there is an important concept called function. Function is a block of code that carries out specific tasks and can be reused in your program. And that’s exactly what we need now.

Let’s use function to rewrite the code.

  1. var math:Array = [70, 80, 65, 90, 85, 100];
  2. var history:Array = [90, 89, 92, 82, 78, 95];
  3. var music:Array = [72, 75, 89, 92, 65, 79];
  4. trace(”============Mathematics==========”);
  5. printScore(math);
  6. trace(”============History==========”);
  7. printScore(history);
  8. trace(”============Music==========”);
  9. printScore(music);
  10. function printScore(subjectArray:Array):void {
  11. for( var i:int = 0; i < subjectArray.length; i++) {
  12. trace(i + “:” + subjectArray[i]);
  13. }
  14. trace(”===============end===============n”);
  15. }

Aha, see the difference? Now, the code is more laconic, and the output format is easier to change, you just need to rewrite the trace statement in the function if you want to change to output format.

For example, if you want to change the output format to the previous one, you just need to modify the statement:

  1. trace(i + “:” + subjectArray[i]);

into:

  1. trace(subjectArray[i]);

then, you can get what you want.

With function, eh, you also can call it subroutine; your can reduce the cost of developing or maintaining your program, especially for large program, while increasing its quality and reliability.

OK, another question is here.

What’s function?

Maybe you’ll quickly remember the concepts of function you learned in your math course when we mention function here. Let’s do some review now J

In the mathematics, the function is often defined as:

  1. f(x) = 2x

or something like that.

After we define the function, f(x), we can know that:

  1. f(1) = 2
  2. f(2) = 4

That means for the some input, the function has a corresponding output, this is the determinacy of function. And, it’s totally the same in programming languages.

For example, if we define the following function in ActionScript.

  1. function multipleByTwo(someValue:int):int {
  2. return someValue << 1;
  3. }

Then, the behavior of this function, multipleByTwo is known to us. If you pass 1 as the parameter into this function, you will get 2, pass 2, you will get 4. You won’t get 5 if you pass 3, that’s the property of function, determinacy.

Maybe, you’ll say what if I define a function with random output, and then the function is not a deterministic one. Eh, in this situation, the function is also determinate, and we all know the output of your function is random J

So, the determinacy of function is mostly about its behavior, which means what you want to do with that function.

In other words, function is use for carry out specific tasks.

When you notice that some code can be organize to a , and maybe it’ll be reusable in the future. Then you can define that code block as a function.

After you define a function, you just need to know its behavior and ignore the internal structure of the function. You can consider the function as a black box, when you put something in, you’ll get something out.

Remember the trace function? We don’t know the internal structure of this amazing function, but we can use it as we want, that’s the power of function.

So, function is a block of code, the behavior is deterministic, use for carry out specific task.