Introduction to Functions

At first glance, functions can be mind-boggling. At the same time, however, they are extremely useful. They help programmers around the world to make their code simpler, faster, and more readable and understandable. That is why it’s important for you to come to grips with the multitude of abilities that functions give you. With that said, lets start with a simple example of what a function looks like:

ActionScript:
function helloWorld (){
        trace ("Hello World!");
}


By using the keyword ‘function’, we are telling flash that we are creating a function(duh). The next word ‘helloWorld’ will be the name of the function. All variable naming conventions apply to function names also. To always be on the safe side, make sure that the first character of the function is in the alphabet “a-z”. Spaces cannot be used in the name, so instead of “hello World”, you must use “helloWorld” or “hello_world”. To sum up the naming standards, you can only use letters, numbers, and underscores and the name must begin with a letter. Next, we have this: ‘()’. In more complex functions, we send information to the function using what we call arguments. This allows functions to be very flexible, and well, useful. When making extremely simple functions, however, we must include the () to avoid flash getting all confused and freaking out(and we don’t want that, do we?). Now, the real meat of the function is found between the ‘{‘(the start) and the ‘}'(the end). I this particular function, we tell flash to spit back the phrase “Hello World!” in the Output window. Up to now, all we have done is define the function. In itself, this function will do nothing but sitting in the memory of your swf file(about 700 bytes to be exact). The good news, is that you can easily put it to use by calling it. This is done by using the phrase we used to name the function, like this:

ActionScript:
helloWorld ();


If you’re sitting there drooling, half asleep, and thinking “boooooooooooooooring”, you’re right. This kind of function has no real-world functionality. so lets extend it a bit by adding an argument:

ActionScript:
function helloWorld (myName){
        trace ("Hello "+myName+"!");
}
helloWorld ("Bob");


As you should quickly see, the function takes whatever you send it as an argument and turns it into a variable that it uses. This is convienent in more ways than one. The most outstanding of these is that in this whole example, the only variable defined you your movie is ‘helloWorld’. You never have to worry about your arguments overwriting other flash variables(unless you want it to, and we’ll get into that later!). For example:

ActionScript:
function helloWorld (myName){
        trace ("Hello "+myName+"!");
}
myName = "Frank";
helloWorld ("Bob");
trace (myName);//the variable myName is still Frank!


As it stands, we are still hardcoding the arguments being sent to the function by entering “ourtext” or something similar. Functions gain even more power, however, when you learn how to send it the content of a variable itself, like this:

ActionScript:
function helloWorld (myName){
        trace ("Hello "+myName+"!");
}
userName = "Bob";
helloWorld (userName);


Just like in the previous examples myName became whatever you sent the function(“Bob”), myName now becomes ‘userName’. That means that we can change the variable userName and call the function and it will change appropriately. Here’s an example:

ActionScript:
function helloWorld (myName){
        trace ("Hello "+myName+"!");
}
userName = "Bob";
helloWorld (userName);//Hello Bob!
userName = "Frank";
helloWorld (username);/Hello Bob!


Alternatively, lets suppose that a whole family is using your application and you want to say hi to them all. Heres an example(requires understanding of Arrays and for loops):

ActionScript:
function helloWorld(myName) {
        trace("Hello "+myName+"!");
}
family = ["Bob", "Sylvia", "Angela", "Tony"];
for (var counter = 0; counter<family.length; counter++) {
        helloWorld(family[counter]);
}


Now, its time to actually get something out of our functions instead of these goofy outputs. Lets say you want to set a variable to equal the result of some calculation. We’ll start with this:

ActionScript:
function amIAdmin(myName) {
        if (myName == "TheDutch") {
                return "yes, I am";
        }else {
                return "no, you sucker";
        }
}
userName = "TheDutch";
nameCheck = amIAdmin (userName);
trace (nameCheck);//yes, I am


A more intuitive, and programming applicable, way of doing this would be to use true/false and to place my users and their values into an array, kinda like this:

ActionScript:
function amIAdmin(myName) {
        if (myName == "TheDutch") {
                return true;
        }else {
                return false;
        }
}
currUsers = new Array();
currUsers[0] = new Object();
currUsers[0].userName = "TheDutch";
currUsers[0].isAdmin = amIAdmin (currUsers[0].userName);
trace (currUsers[0].isAdmin);


Doing this will give me more freedom in future programming when I have more than one user and need to check it they have any… ‘powers’… hehe

Lets suppose that we also need a login script before adding the user to the ‘currUser’ list. Lets add another function to do that:

ActionScript:
//will compare myName and myPass for validity
//if correct, it will add these values to the array you send(userList)
//it then returns that it worked
//if incorrect, it will simply tell you that it didn't work.
function checkLogin(myName, myPass, userList) {
        //name and pass check
        if (myName == "TheDutch" && myPass == "iRock") {
                // an array with one value will have a length of 1
                // this value will be a yourArray[0], however
                // logically, the next open space is yourArray[1]
                var nextSpace = userList.length;
                //we will make this open space an object
                //this works similar to an array, but its broader
                userList[nextSpace] = new Object();
                userList[nextSpace].userName = "TheDutch";
                //notice that I can call a function from inside another function
                userList[nextSpace].isAdmin = amIAdmin(userList[nextSpace].userName);
                //everything went ok, good
                return "Added Successfully";
                //if the name and password don't match
        }else {
                //say that there is something wrong
                return "Wrong Username or Password";
        }
}
function amIAdmin(myName) {
        if (myName == "TheDutch") {
                return true;
        } else {
                return false;
        }
}
currUsers = new Array();
//sending the checkLogin function the username, password...
//I'm also sending the location of my list of users..
//allowing the function to modify it
//'success' will equal whatever the function returns
success = checkLogin("TheDutch", "iRock", currUsers);
trace (success);//Added Successfully
success2 = checkLogin("TheDutch", "hacker", currUsers);
trace (success2);//Wrong Username or Password

Leave a comment