Skill Sets:

Home

  1. Intro to Flash
  2. User Input
  3. Fancy Controls 1
  4. Control Flow: if
  5. Control Flow: loop
  6. Nested Structures
  7. Graphics 1
  8. Arrays
  9. Strings
  10. Mouse Movement
  11. Functions
  12. Grouping Images
  13. Graphical Support
  14. Animation
  15. Physics
  16. 2010 Grade 10 Summative showcase
  17. 2010 Grade 11 Summatives
  18. 2011 Grade 10 Summatives
  19. 2011 Grade 11/12 Summatives
  20. Sept 2011 Grade 11 Summatives
  21. Sept 2011 Grade 10 Summatives
  22. Feb 2012 Grade 10 Summatives
  23. Sept 2012 Summatives
  24. Feb 2013 Grade 12 Summatives
  25. Feb 2013 2OI Summatives
  26. Sept 2013 Summatives

Functions

Functions allow you to clearly define a part of your code for re-use. No matter what you are programming, if you are seeing blocks of code repeated, you should be asking yourself, "Is there a better way?". Sometimes the solution to repeated code is an intelligent "loop" structure. Sometimes, if you see yourself copy and pasting blocks of code with slight modifications, it will be time to ask yourself - "Is this a good place for a function?".

Functions allow you to group your code in organized packages. Properly defined functions add clarity and flexibility to your overall program. You will notice when your create your first programs with functions that you will have been able to do the program more easily without them. You would be correct.

Other programming languages call functions "methods". Java calls "functions" "methods".

You have already been using functions.

Everytime we have been coding with "EventListeners", we have been using functions. Now, we will use the EventListener functions to call a function of our own creation.

 

Your first programs will be very similar to previous ones, so that you are not caught up on what to do, but more on "how" to do it. Some students might call this "make-work". I disagree. I would not want you to initially to focus on both "what to do" and "how to do it". I will risk insulting my more intelligent students here. If you feel insulted by some of the exercises in this component, you may be intelligent.

An Example:

In this example, I package some previous code. Here, the EventListener will take a number from a TextInput box, and instead of doubling the number here, we will pass it to the "doubleUp" function and the function will return the answer. First, the EventListener function:

public function addEm(e:MouseEvent):void {
 nNum=int(txtNumber.text);
 nSum = doubleUp(nNum);
 txtSum.text="The sum is "+String(nSum);
}

Notice that this funtion is calling the "doubleUp" function. It is passing "nNum" to doubleUp, and doubleUp will return the anser into "nSum" (which is the variable to the left of the equal sign). Here's the doubleUp function:

public function doubleUp(nNumber:int):int {
 nDouble=nNumber+nNumber;
 return nDouble;
}

See how the doubleUp function accepts an "int" variable to be passed to it. A copy of nNum from the "addEm" function is passed into nNumber in the "doubleUp" function. This function is designed to return an integer back to the function that called it. That's why we complete the function signature with a :int. After nNumber is doubled, it is "returned" to the calling function.