Using Function: The Beginner's Nightmare - Function statement
(Page 2 of 5 )
Since this article is not about every variants of practical using of functions, I will touch only the foundation. What are the functions? Here is a simple introduction. In flash Actionscript function comes in a simple syntax:
function myFirstFunction(param ,param 2, …,param n){
set_of_statements;
}
Function declarations start with keyword 'function', followed by function name and the set of parameters, if any, to be passed to function. You can also setup a function without required parameters; it could be invoked without them.
We can invoke the function by simply using its name as a statement and providing parameters as necessary.
myFirstFunction(param 1, param 2,..., param n);
The number of parameters is up to you. After executing the function, your program will return to the program line right after the line that invokes the function. Let’s see the practical example:
//**set up the function that will add two numbers:
function sum_number(num1, num2){
result=num1+num2;
trace("The result is " add result);
}
To use that function, we will invoke it like this:
sum_number(2, 6);
The output window will show:
The result is 8
This is just the simple example. Functions can be more complex and can contain all kind of actions, calculations and other creative solutions.
You can easily use functions to organize and modularize your code flow. While building an application, you can use functions to simplify your work by placing function on some central place, for example, the main timeline.
Functions have many different aspects of use. They can easily be nested one within another; they can use them as prototypes to define new methods and properties for the objects etc.
Next: Do you like functions? >>
More Flash Articles
More By Aleksandar Horvat