Functions in JavaScript
Preface
In many cases, the program will repeatedly complete the same or similar tasks during execution. To avoid repeated compilation of the same code segment,
JavaScript wraps part of the code into a module that can be reused. It is called a "function ". Functions can be used in other parts of the program, just as they are
The components of the JavaScript language are the same.
Using functions makes the code easier to read and maintain. A function is one of the basic modules of JavaScript and almost appears in every script.
. 1 basic syntax
Creating a function is like creating a new JavaScript command, which can be used in other parts of the script. Keywords can be used to create a function.
Funcition, followed by the name of the function, followed by a pair of parentheses, followed by a pair of curly braces. The curly braces are the JavaScript language that forms the function.
Example:
<Script> function showAlert () {alert (Hello JavaScript)} </script>
. 2 call a function
During page loading, the Code contained in the function definition area will not be executed, but will be executed when called. To call a function, you only need to use the function name.
You can execute the code of the function as needed, as shown in the following example:
<Script> function showAlert () {alert (Hello JavaScript)} </script>
The result is as follows:
Place JavaScript code in the area of the page
So far, all of our examples have placed JavaScript code on the HTML page
Region. In order to better play the role of the function, I
We need to take a more appropriate approach, that is, place JavaScript code to the page
Region. <Script>
Element, or in the pageWhen the src attribute of the <script> element of the region points to an external file, it can be called from any location on the page
. Putting functions in the head of the document ensures that they have been defined before they are called. For example:
<Script> function showAlert () {alert (Hello JavaScript)} </script>
. 3 parameters
If a function can only perform the same operation for each call as in the previous example, its application will be greatly limited. Good news
We can extend the functions of a function by passing data to the function. The implementation method passes one or more "Parameters" to the function when calling the function. See the following example
Example:
<Script> function sum (a, B) {alert (a + B) ;}</script>
We pass two parameters and use alert to display the result of adding two numbers.
The effect is as follows:
. 4 return values from the function
The preceding example shows how to pass parameters to a function to process the data. How can we obtain data from the function? After all, I
You cannot obtain function results only in the pop-up dialog box. To return data from a function, we can use the keyword return. For example:
<Script> function sum (a, B) {var num = a + B; return num ;}</script>
The above uses the onClick event to call the function sum and input two parameters. The function sum returns the sum of the two values through return and uses the document Object
Write method to output data to the page. (The var keyword is used to declare variables)
The effect is as follows: