A function is a set of instructions (a block of code) that are executed only when you access the functions.
Functions allow you to organize your code into independent pieces that can accomplish different tasks.
Functions can be used more than once within a script to perform their task. Rather than rewriting the entire block of code, you can simply call the function again.
There are two general kind of functions:
- predefined - which are included in the programming language (e.g. trace(), addChild(), etc.)
- created by programmer:
- that returns a value (uses the "return" statement)
- not returns a value
Creating a Function
Functions are defined (created) using a special predefined keyword,
function, followed by the function name, a set of parentheses and the function body (within curly brackets).
There are two syntaxes to define a function, with, or without specifying the
DataType.
-
The simple form (without DataType)
function functionName(param1, param2, ...) {
// code to execute (the function body)
}
-
The complete form (with DataType)
function functionName(param1:DataType, param2:DataType, ...):fDataType
{
// code to execute (the function body)
}
-
functionName - is the name of the function. It is used when you want to run (access) the function.
-
param1, param2, ... - is an optional list of parameters.
-
DataType - specifies a type of data that the associate element can contains.
-
fDataType - (after the parentheses) sets the type of data that the function can return. If the function doesn't return a value (not use the "return" instruction), you can use
void for
fDataType.
Example of a simple function:
function getMsg(msg:String) {
trace(msg);
}
- This function has a parameter,
msg, which is defined to contain String data type. When you'll call this function, the code within curly brackets is executed (
trace(msg)).
Calling a function
To execute the code of a function you must call (or invoke) it, by using the function's name followed by a pair of parentheses ().
- If the function is defined without parameters, use this syntas:
functionName()
- If the function has parameters, use the following syntax:
functionName(arg1, arg2, ...)
-
arg1, arg2, ... - are the arguments (values) passed to the function, in order, to function's parameters. The number and type of values passed in must match the function definition.
You can call a function multiple times. The code of the function is executed in the location where we invoke it.
In the next example we create a function with a String parameter, and then we call it two times (using a direct value for the argument, and a value contained in a variable):
// define the function
function getMsg(msg:String) {
trace(msg);
}
var site:String = 'coursesweb.net'; // define a String variable
// calling the function two times
getMsg('Welcome to');
getMsg(site);
When the
getMsg() function is called, the value of the argument is passed to the function.
The function stores this value in the "
msg" parameters, and use it in its code.
- If you add this function in the Action panel of a new Flash document, and press "Ctrl+Enter", the Output panel will show the following result:
Welcome to
coursesweb.net
Using the return
A
return statement is used to specify a value that a function returns when is called.
The "
return" keyword must be followed by the value that you wish to return.
The syntax for creating a function that returns values is:
function functionName(param1:DataType, param2:DataType, ...):fDataType
{
// code to execute
return someValue;
}
-
someValue can be a primitive (a string, a number, a boolean), a variable, or a expresion.
- The "
fDataType" is optional. If you add it, the data type of "someValue" must match the "fDataType".
You place the return statement as the last line of the function (before the closing curly bracket), becouse when a return statement is executed in a function, the function ends and the rest of the code is not processed.
The value returned by a function can be assigned to a variable, like in the following example:
// define a function with two numeric parameters, and returns the value of a variable
function addAB(a:Number, b:int)
{
// sets a variable to store the result of a+b
var c:Number = a+b;
return c; // return the value of 'c'
}
// calling the addAB() and store the result in a variable
var adds = addAB(7.5, 8);
trace(adds); // output the value of "adds": 15.5
- The "
adds" variable stores the value returned by the function. The trace function will output this value:
15.5.
You can use the return statement in any function, with, or without parameters.
Defining a return type
When you define a data type for a parameter with the colon (:) after the parameter name, it indicates what kind of information should be stored in the parameter.
When you add a DataType after the parentheses of a function, it specifies wat kind of data the function can return.
The Data Type can be any kind of data used for variables (String, Number, int, ...).
If you are unsure what type of data the function will return, you can use the wildcard data type (*), which represents untyped data. This will allow the function to return any type of data.
In the next example we use the same function defined in the example above, but this time we set a data type for the value that the function will return.
// define a function with two numeric parameters, and returns a int data type
function addAB(a:Number, b:int):int
{
// sets a variable to store the result of a+b
var c:Number = a+b;
return c; // return the value of 'c'
}
// calling the addAB() and store the result in a variable
var adds = addAB(7.5, 8);
trace(adds); // output the value of "adds": 15
- "
:int" after the function's parentheses force the function to return only integer values.
- 7.5+8 = 15.5, but the function returns only the integer part (15).
So, if you specify or not a return DataType can determine the result of the function.
• Functions which not have a return statement can use a data type of
void.
function functionName(param1, param2, ...):void
{
// code to execute, but without return
}