First, the concept of function
Daily life, we want to complete one thing, always used to have a plan, the latter according to plan, step by step implementation, then can be completed, and achieve a certain effect to achieve certain functions. In the programming world, "function" can be called "function", so "function" is a piece of code that implements some function , and can be called by other code.
In JavaScript, functions are event-driven, or reusable blocks of code that are executed when they are invoked.
For how JavaScript works, you can refer to how JavaScript works
For example:
Second, JavaScript function syntax
In JavaScript, a function is a named block of code that is decorated with the keyword "function". The format is as follows
Compare the previous examples to find out the ingredients; Define your own method, pop up the display box, and show your name.
Tip: JavaScript is case sensitive. The keyword function must be lowercase.
Iii. defining a function with parameters
When we execute the method, we need to provide some external data as raw material, we need to give a name (formal parameter) when the method definition, as the parameter identification, in the method body can use the data by name. When called, the data is passed in as needed (arguments), and the actual data is used in the location of the method as it is executed.
parameter: The data passed in when the method is defined, just the name. Use the name in the method body.
arguments : When the method is called, the data is angry, the real data, and the data is manipulated when the method executes.
hint : Whether it is a formal parameter or an argument, there can be any number, if there are more than one parameter, then the argument with the parameter "," Split, the last parameter after the ",".
Iv. functions with return values
This method should have a return value when we need to get a result when some of the methods have been run out. Use the "return" keyword in the method body to give the final result.
Tip: When there is a "return" statement in the method body and the data is returned, the calling method can receive the result in addition to the variable, and when there is no "return" statement in the method body, the variable cannot be used to receive the result when the method is called.
V. Summary
method is used to improve the reusability of code. When a particular function needs to be completed, we can combine the code that completes the function, name the block of code, and decorate it with the function keyword, where the code block becomes a method. This method can be called by the name of the method in other places where it is needed. Method must be called before it is executed.
The function of return: 1, send the result; 2. Early Termination method
After learning should achieve the following 3 effects, even if you are done!
First: can define a method;
Second: can invoke the method;
Third: can return the result in the method body;
Indispensable functions in the JavaScript work system