<> Introduction of function

function : Is to encapsulate some functions or statements , When needed , By calling , Execute these statements .

* A function is also an object .
* use typeof When checking a function object , Will return function.
<> Functions :

* Write a large number of repeated statements in functions , When you need these things in the future , You can call the function directly , Avoid duplication of effort .
* Simplify programming , Modularize programming .
example : function addN(n){ var sum = 0; for(var i=0;i<=n;i++){ sum = sum + i; }
document.write(sum); }
<> Function definition and call

The first step : Definition of function
** Mode one :** Use the function declaration to create a function , grammar :
function Function name ([ Formal parameter 1, Formal parameter 2,... Formal parameter N]){ sentence ... }
give an example :
function sum(a,b){ return a+b; }
The explanation is as follows :

* function: It's a keyword , Chinese is “ function ”,“ function ”;
* Function name : The naming rules are the same as those for variables , It can only be letters , number , Underline , Dollar sign , Cannot start with a number ;
* parameter : Optional ;
* Inside the braces , Is the statement of this function .
**PS: In some compilers , After the method is written , We enter in front of the method /, Then enter , You will find that the format of the comment will be automatically completed .
** Mode 2 :** Use function expressions to create a function , grammar : var Function name = function([ Formal parameter 1, Formal parameter 2,... Formal parameter N]){ sentence ... }
give an example :
var fun3 = function(){ console.log(" I'm the code encapsulated in anonymous functions "); };
It can be seen from the example of mode 2 : So called “ Function expression ”, In fact, it is to assign an anonymous function to a variable , of course , We have three more ways : Use the constructor to create an object , But this method is less used .

<> Step two : Function call

Syntax of function call :
Function name ();
<> Parameters of function : Formal and real parameters

The parameters of the function include formal parameters and actual parameters , Suppose you define a function that is summated .
Formal parameter :

*
You can use the () To specify one or more formal parameters .

*
Use between multiple formal parameters , separate , Declaring a formal parameter is equivalent to living the corresponding variable inside the function , But it's not assigned .
Actual parameters :

*
When a function is called , Can be in () Specify arguments in .

*
The argument is assigned to the corresponding parameter in the function .
give an example :
// Call function sum(3,4); sum("3",4); sum("hello","world"); ,, Define function , Sum up function sum(a,b){
console.log(a + b); }
Console output :
7 34 helloworld
Types of arguments :
The arguments to a function can be of any data type .
The calling function parser does not check the type of the argument , So pay attention , Is it possible to receive illegal parameters , If yes, you need to check the type of the parameter .
Number of arguments

be careful :JavaScript It does not detect the actual incoming parameters , No value can be transferred , You can also transfer multiple values arbitrarily ;

When calling a function , The parser also does not check the number of arguments .

* Extra arguments are not assigned .
* If the number of arguments is less than the number of formal parameters , Then the formal parameter without corresponding argument will be undefined, for example : function add(a,b){ console.log(a);
console.log(b); return a+b; } // add(3,4);// add();
The results are as follows :

* If the number of arguments is more than the number of formal parameters , It will count from front to back to the same number of parameters , stop it ;
* Can pass arguements Gets all the parameters actually passed :
for example : function add(){ //arguments: You can get all the parameters that are actually passed ; console.log(arguments); } add(1
,2,4,5,3,6);
give the result as follows :

<>over

Technology