Search Tutorials
Functions
A function is a named unit, in which statements are logically grouped. It usually contains the following parts:
Function functionName(arg1, arg2, arg3){
// statements go here
return something if not void return type;
}
example1 (AS 1.0). Type this and compile (Control+Enter):
function test1(arg1, arg2){
// arg1 is passed to the variable with the same name
// in the next line I.e. arg1
trace(arg1);
trace(arg2);
}
// call function
test1(3,"Steve");
Notice that to call a function we write the function name and the argument lists in parentheses.
Template to call a function:functionName(arg1, arg2);Example 2 with a return value (AS 1.0)
function returnTest(arg1){
var num = arg1 * 10;
return num;
}
// call function
trace(returnTest(5));
ex. 3
// returns the absolute value of number
function abs(number){
return( i <0 ? -i : i);
}
// call the method
trace(abs(-350.95);
ex 4
// return the smaller of two values.
function min(num1,num2){
return(num1 <= num2 ? num1: num2);
}
trace(min(27,45));
Functions in AS 2.0
Functions in AS 2.0 are a little different. AS 2.0 allows the setting of specific datatypes.
Here is an example :
Function avgReturn(arg1:Number):Number{
Var num = arg1 * 5;
return num;
}
The template for an AS 2.0 function could be :
Function functionName(arg1:DataType,
arg2:DataType):ReturnType{
// statements go here
return something ;
}
Void is used to specify that there is no return type. E.g.
function noReturn(arg1:Number,
arg2:String):Void{
trace(arg2+" is a string and "+arg1+" is a number");
}
// call function
noReturn(5,"Steve");
Arrays
An array is a collection of objects . Individual objects are not named but accessed by its position in the array.
Try this!
var theArray = new Array(5,4,8,2,1);
for(var i = 0;i<5;i++){
trace(theArray[i]);
}
Elements are numbered beginning with "0". In the above array theArray[0] = 5 theArray[2] = 8 Length is a property of Array. It tells how many elements are in the array. So , from above, theArray.length = 5 Classes
(As 2.0 only)
class Dog{
// data members
var name:String;
var age:Number;
// class method
function setAge(age1:Number){
age = age1;
}
}
Constructors allow us to create an instance of a class . e.g.
var rusty:Dog = new Dog();
// the dot operator (".")
// allows us to access members and methods of that class.
rusty.setAge(7);
rusty.name = "Rusty";
For more information and tutes , go to my site.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|