All values in JavaScript are called variables. The reason for this is that the values "vary". Anytime you create a variable, you use var to declare the variable.
A function is a group of procedures that allow you to treat JavaScript statements as a single unit. Functions are created in the HEAD of the document. Below is a typical function:
function add_one_to_num(num) {
num = num + 1;
}
The word "num" in parentheses is called a parameter. A parameter is a variable that a function uses. Not all functions require parameters. A function that executes once and is done does not need a parameter to function. The curly braces { and } contain the variables and other information that the function will use.
To execute a function, you must call the function. To do that, you create a gereic version of the function in the BODY of the document using the function name followed parentheses and any variables required to be sent to the function. To call the function we created above, we would type this:
add_one_to_num(36);
This would give us the number 37 because the call to the function sends 36 and the function adds 1 to it to get the number 37. This is how the call to a function operates. Sometimes these two work in opposite directions. In this case, you would type:
var returnValue = average_numbers(1, 2, 3);
Next, you would create a function to return the value back to the calling function.
function average_numbers(a, b,
c) {
var sum_of_numbers = a
+ b + c;
var result = sum_of_numbers
/ 3;
return result;
}
This will give us the answer 2 because sum_of_numbers adds the numbers up and becomes 6, then result takes sum_of_numbers and divides it by 3.
Question: Sounds easy enough. So where do the events part of the programming come in?
Answer: Now that you have a general understanding of this part of JavaScript, we can move on to events.
Quick Links:
Have Java Problem
Do
you have a Java Question?
Java Books
Java
Certification, Programming, JavaBean and Object Oriented Reference Books
Return to : Java
Programming Hints and Tips