CD-51Chapter 7 .Programming Fundamentals, Part II Functions In (Web host music)
CD-51Chapter 7 .Programming Fundamentals, Part II Functions In Chapter 5, you saw a preview of the JavaScript function. A function is a definition of a set of deferred actions. Functions are invoked by event handlers or by statements elsewhere in the script. Whenever possible, good functions are designed for reuse in other documents. They can become building blocks you use over and over again. If you have programmed before, you can see parallels between JavaScript functions and other languages subroutines. But unlike some languages that distinguish between procedures (which carry out actions) and functions (which carry out actions and return values), only one classification of routine exists for JavaScript. A function is capable of returning a value to the statement that invoked it, but this is not a requirement. However, when a function does return a value, the calling statement treats the function call like any expression plugging in the returned value right where the function call is made. I will show some examples in a moment. Formal syntax for a function is as follows: function functionName ( [parameter1]…[,parameterN] ) { statement[s] } Names you assign to functions have the same restrictions as names you assign HTML elements and variables. You should devise a name that succinctly describes what the function does. I tend to use multiword names with the interCap (internally capitalized) format that start with a verb because functions are action items, even if they do nothing more than get or set a value. Another practice to keep in mind as you start to create functions is to keep the focus of each function as narrow as possible. It is possible to generate functions that are literally hundreds of lines long. Such functions are usually difficult to maintain and debug. Chances are that you can divide the long function into smaller, more tightly focused segments. Function parameters In Chapter 5, you saw how an event handler invokes a function by calling the function by name. Any call to a function, including one that comes from another JavaScript statement, works the same way: a set of parentheses follows the function name. You also can define functions so they receive parameter values from the calling statement. Listing 7-1 shows a simple document that has a button whose onClick event handler calls a function while passing text data to the function. The text string in the event handler call is in a nested string a set of single quotes inside the double quotes required for the entire event handler attribute.
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.