Saturday, 10 January 2009

Functions, functions, functions

Being originally trained in graphic design, I missed out on formal training in computer programming. The subtleties of programming were all self taught and learned as needed, rather than learned in a logical and structured way. This, and the fact that I come from an art background (definately part of seeing the world a little differently) perhaps explains why I tend to come onto programming solutions to my interactive media problems in a round-about way.

The purpose and value of functions is a perfect example of this. A programmer could probably explain it to me until they were blue in the face, but in fact the best explanation I have ever had came accidentally in two halves, while actually looking for a solution to something else.

The following is actually bits taken from two different chapters in the Flash MX 2004 ActionScript reference ("Using built-in functions" and "Returning values from a function"). Why they didn't just explain it this way in the first place I will never know. Still, if having it explained this way makes sense to you I share it below for your benefit:

A function is a block of ActionScript code that can be reused anywhere in a SWF file. If you pass values as parameters to a function, the function will operate on those values. A function can also return values.

To call a function, simply use the function name and pass any required parameters.

For example, the following function returns the square of the parameter x and specifies that the returned value must be a Number:

function sqr(x:Number):Number {
return x * x;

Some functions perform a series of tasks without returning a value. For example, the following function initializes a series of global variables:

function initialize() {
boat_x = _global.boat._x;
boat_y = _global.boat._y;
car_x =;
car_y =;


  1. I have to ask, Dan, how exactly was it described to you before? I can't really think of any *other* way of describing a function.

  2. Once again I totally agree. The issue was that this explanation did not appear in this form in the book. I put it together from two tidbits of info that were in separate parts of the book.

    I wonder why they thought it was better explained in two halves.

    All I did was bring the two halves together and finally it began to make sense.