| Introduction to ActionScript 2.0 | ||
| Operators | Functions | Properties and Methods |
Key concepts:
Now that we've learnt all our operators, we can get to the next part: functions.
In mathematics, a function consists of an input, a process and an output. For example, if x = 3 is input into f(x) = 2x, we get the output 6.
In computers, afunction is a collection of statements. When a function is executed, all the statements inside the function will be performed. A function may or may not have inputs, and a function may or may not have outputs. (The latter, of course, cannot be said of spreadsheet functions, which must have outputs.)
Here's a simple function with no outputs and no inputs. Look at the code and we'll explain it step by step:
| Code | Result |
|---|---|
varsomeNumber:Number=5;functionaddOneToSomeNumber():Void{someNumber++;} |
Firstly, the first statement is a simple variable declaration statement we've already learnt before.
The second line has various elements:
function is added to the beginning of a function declaration just asvar is added to the beginning of a variable declaration.:Void part is for the output of the function. Normally,Void would be replaced by the data type of the output. Since this function involves no outputs, we use the keywordVoid instead.A block refers to a group of statements enclosed by a pair of braces. These statements are to be performed in that order. In a function, the block is known as thebody of the function. Later, we'll learn other applications of the block. Remember that we always indent the statements in a block. This is automatically done for you in the Flash API.
In order to use a function, we need tocall it. Let's look at the above example again:
| Code | Result |
|---|---|
varsomeNumber:Number=5;functionaddOneToSomeNumber():Void{someNumber++;}addOneToSomeNumber();trace(someNumber); |
|
In the function call above, we just called the functionaddOneToSomeNumber. The bracket after it is for the inputs of the function, which we'll cover in the next section. The computer performed the function, which involves increasing someNumber by 1. That's how we got the result 6.
In programming, the input of a function is called aparameter orargument.[1] Let's modify our addOneToSomeNumber function to accommodate the addition of any number to someNumber:
| Code | Result |
|---|---|
varsomeNumber:Number=5;functionaddAnyNumberToSomeNumber(anyNumber:Number):Void{someNumber+=anyNumber;}addAnyNumberToSomeNumber(2);trace(someNumber);trace(anyNumber); |
|
In this example, the codeanyNumber:Number is put into the brackets. This is the input of the function and is written asvariableName:DataType.[2] Once input, anyNumber is known as alocal variable. This means it cannot be referred to outside the function. That's why we could not trace anyNumber at the end.
Note that this does not result in an error, but the keywordundefined. Whenever we try to refer to a value that is not defined, Flash will return the valueundefined. We will discussundefined and its twin brothernull later.
To call the function addAnyNumberToSomeNumber, we only need to put the value inside the bracket. The data type is not needed!
Now let's modify our code to have two inputs:
| Code | Result |
|---|---|
varsomeNumber:Number=5;functionaddTwoNumbersToSomeNumber(firstNumber:Number,secondNumber:Number):Void{someNumber+=firstNumber+secondNumber;}addTwoNumbersToSomeNumber(5,3);trace(someNumber); |
|
The comma is used to separate the two parameters. This also works with three parameters or more.
Look at our addAnyNumberToSomeNumber code again. Suppose we don't want to touch the original variable, but want to get the sum of anyNumber and someNumber anyway. That's where a function with a return value comes in handy.
| Code | Result |
|---|---|
varsomeNumber:Number=5;functionaddTwoNumbers(originalNumber:Number,anyNumber:Number):Number{return(originalNumber+anyNumber);}trace(addTwoNumbers(someNumber,7)); |
|
In this function, the value of someNumber was passed on, but instead of changing someNumber, the function returns the sum of someNumber and 7 directly to the trace function.
Note that since this function contains only a return statement and nothing else, it would not make sense to call the function as a separate statement:
| Code | Result |
|---|---|
varsomeNumber:Number=5;functionaddTwoNumbers(originalNumber:Number,anyNumber:Number):Number{return(originalNumber+anyNumber);}addTwoNumbers(someNumber,7); |
|
See, nothing happens! The function is good for returning a value, but nothing else.
Also note that functions in return values can be used in a variety of versatile ways that is not limited to tracing. For instance, look at the following code:
| Code | Result |
|---|---|
varsomeNumber:Number=5;functionaddTwoNumbers(originalNumber:Number,anyNumber:Number):Number{return(originalNumber+anyNumber);}varyetAnotherNumber:Number=6/addTwoNumbers(someNumber,7);trace(yetAnotherNumber); |
|
In this script, the computer first evaluates the value addTwoNumbers(someNumber, 7), which equals 12. It then performs the operation 6 / 12, and finally assigns it to yetAnotherNumber.
To conclude, the syntax for declaring a function is as follows:
functionfunctionName(parameter1:DataType1,parameter2:DataType2...):ReturnDataType{Statements; returnvalue;}Like variables, you cannot use reserved words for functions, and the functions can only start with a letter. According to established naming conventions, good function names start with an action verb which can be concatenated with other words using CamelCase. Examples include eatWatermelon(), drinkWater() and smashVase().
When a function is called, it is called acallee. When a function is called by another function, the calling function is called thecaller.
Thearguments object of a function can find the caller and the callee. The following example traces the caller and callee:
| Code | Result |
|---|---|
functionuselessFunction():Void{trace(arguments.caller);trace(arguments.callee);}uselessFunction(); |
|
Note that since functions cannot be represented in textual form, the computer traces [type Function], which just indicates that it's a function.
If you really want to, you can put a function inside a variable. Here's the syntax:
var variableName:Function = function(parameter1:DataType1,parameter2:DataType2...):ReturnDataType{ Statements; return value;}Here's an example:
| Code | Result |
|---|---|
varsomeFunction:Function=function(someString:String):Void{trace(someString);}someFunction("Hello world!"); |
|
By now, you may be wondering where you got thetrace function when you've never defined it anywhere. Read the next chapter to find out!