Introduction to Programming Page 5 |
Page 5 What's Your Function?
Any JavaScript, C++, or Perl program is built with functions. Functions are expressions that execute an operation, allowing a programmer to create several pieces of code and bunch them together to perform a particular task. Functions accept information in the form of arguments (the values that are passed through the function) and evaluate the arguments to provide results. It's important to prevent mix-ups by differentiating between arguments and parameters. These terms are often used interchangeably, which has always been especially confusing for me. So, to make things clearer, when I say arguments I'll be referring to functions. Likewise, when I say parameters, I'll be referring to programs. Arguments are created whenever a function is defined. They are basically placeholders for the values that will be passed through the function. When an argument is passed through the function, it is evaluated and then applied to the function, which gives you a return value (the results). Arguments exist only during the life a function and change every time it's called again because the values passed through them also change. The parts of a function are the function name, the argument(s), and the body. It looks something like this:
function printName(argument)
{
body (all the cool code)
}The function name is printName and it takes in one argument. The stuff between the two curly brackets is the body and that's where all the code goes.
There are two types of functions, built-in and user-defined. Built-in functions are the functions that already exist within the language. An example of this in JavaScript would be alert () ordocument.write(). User-defined functions are functions that the programmer creates. They are defined by assigning a name to the function and executed by calling that name again. The exact process of defining them varies from language to language. In JavaScript, you begin with function. Let's look at a function that combines everything we have learned so far. Basically, when you enter the page, you type in a password and get a response according to whether or not the password is correct. The code would look like this:
function getPassword () {thePasswords = new Array('foo','bar','bobo');
var passwordMatches = false;
var enteredPassword = prompt ("Enter your password:");
for (i=0; i < thePasswords.length; i=i+1) {
if (enteredPassword == thePasswords[i]) passwordMatches = true;
}
return passwordMatches;
}
var theVerdict = getPassword();
if(theVerdict == true) {
alert("Right-O!")
} else {
alert("How much schooling did you say you had?");
}
As soon as you type in a password it becomes the value of the variable enteredPassword. If you type in a password - "bobo" for example - the function sifts through the array (thePasswords) and compares each of the elements to the password you typed in. If it finds a match, the value of the variable passwordMatches changes to true, which also makes the function getPassword true, so you would then get a pleasant little message. If you type in a password like "spivey," the function would be false because it doesn't match an element in the array, so the value of passwordMatches (which is initially false) would not change.In the loop in this function, the initial value of the variable i is 0. Each time the loop moves to a different element in the array, it adds one to the value of i. This is due to the i=i+1 piece of the loop. The position of each new element is one more than the last. If i=2 or thePasswords[2], then it would compare your password to "bobo" in the array list because that is the second position in the list (the third counting from 0). Basically, thePasswords[2] is equal to "bobo." If there was another element in the list after "bobo," it would be referred to as thePasswords[3], since it would be the fourth position in the array. So, if i=0 and the corresponding element in the array does not match your password, it will continue to sift through the rest of the list and compare. As long as i < thePasswords.length is true, the loop will continue to add one to i and compare until the expression returns false (when i is greater than or equal to thepassword.length). If i < thePasswords.length is false, the function will return whatever value passwordMatches is set to - which could be either true or false. If passwordMatches is false then you'll receive a not-so-pleasant message.
BreBru.com Information Technology HTML