Page 2 of 7
To differentiate between functions and variables, I prefer to give my variables initial upper case characters, such asMyStuff. This immediately differentiates it from a function, which would use a the capitalizationmyStuff. Of course, you are free to adopt any capitalization scheme you wish.
The best way to describe the how and why of a function is to show a simple one in action. Here's a basic function that displays "Hello, JavaScripters!" and is an obvious takeoff on the "Hello World!" example you see for new programming languages.
function basicFunction () {
alert ("Hello JavaScripters!");
}
This merely defines the function. JavaScript will do nothing with it unless the function is referenced someplace else in the script. You have to call the function in order to use it. Calling a user-defined function is the same as calling a built-in JavaScript function -- you merely provide the name of the function in your script. This serves as the function call. When JavaScript encounters the function call, it dashes off to complete whatever instructions are in that function. When the function is over, JavaScript returns to the point immediately after the function call, and processes the remainder of the script.