In this chapter, you'll learn how to: Write your own functions Use homemade functions to improve your code Write functions you can cut and paste into whatever pages you want We'll be using homemade functions in every chapter from now on, so pay extra-close attention to what's going on in this chapter.
Functions as Shortcuts Functions aren't anything new. The alert function, for example, takes whatever text you put inside the parentheses and displays an alert box with that text.
The functions you create act as shorthand as well. Let's say you want to write a link that opens a small window and then centers that window on the screen if the visitor is using Netscape 4. You could write a link resembling Figure most of the code in it is similar to Figure A link that opens a small window and centers it in Netscape 4 and above-- this won't work in Internet Explorer see note at the end of Chapter 5 However, it is not a good idea to write a link in this way: Furthermore, if you want two or three links on your page, your HTML becomes even uglier and your page's download time increases.
Even more problematic, if you want to change the code to affect window size or centering, you have to make the change everywhere you put the link.
Make sure you don't give a function and a variable the same name. Because of this weird behavior, and because function names are case sensitive, it makes sense to have a different convention for naming functions than for naming variables.
For variables I use lowercase letters with underscores, and for functions I use what's called in-caps or camel-caps notation. Names in this notation style consist of strings of words without spaces, in which every word except the first is initial-capitalized, as in openAndCenterTheWindowmyCatand printDate.
In-caps notation is a pretty common convention and should serve you well. Parentheses and Curly Brackets A pair of parentheses follows the function'sname. For now, you won't be entering anything between them, but they're still necessary. After the parentheses you need a pair of curly brackets.