Author: steve happ | Email
Website : www.video-animation.com
Advertisement
Functions
A function is a named unit, in which statements are logically grouped. It usually contains the following parts:
Function Name - e.g. function someName( )
Argument list - is enclosed within parentheses after the function name. It contains a comma-separated list of 0 or more arguments. Arguments are passed to the variable with the same name as the argument inside the function.
Return Type - the datatype of what the function can evaluate to.
The function Body - is enclosed within a pair of braces { } and contains a sequence of statements.
Here is a template. For AS 1.0 :
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));
We hope the information helped you. If you have any questions
or comments, please don't hesitate to post them on the
Forums section
Submit your Tutorial at Click Here