Function 【function】

A. [Function definition]

Encapsulate code that performs a specific function

The format of the complete function

    functionThe function name (parameter1The parameter3..){codereturnThe return value}Copy the code

Functions are not automatically executed and need to be called manually

Function call:

Function name (argument1That argument2...) ;Copy the code

If a parameter is defined but no argument is passed, the default value of the parameter is undefined;

Undefined is returned by default if return is not added

If a function has a return in it, the function stops immediately after the return

Define a function that finds the sum of two numbers

    function add(a, b) {
        var sum = 0;
        sum = a + b;
        return sum;
    }
    var sum = add(2.3);
    console.log(sum);
    console.log(add(70.80));
Copy the code

Function that takes no parameters and returns a value

    function print(){
         console.log("Hello World!!!");
    }

    print();
    print();
Copy the code

If a parameter is defined but no argument is passed, the default value of the parameter is undefined;

If a function has a return in it, the function stops immediately after the return

    function print(arg){
        console.log(arg);
        // return;
        // console.log(12323332);
    }
    print("Hello");
    print("Thoughts of the Still Night");
Copy the code

If a parameter is defined but no argument is passed, the default value of the parameter is undefined;

Undefined is returned by default if return is not added

If a function has a return in it, the function stops immediately after the return

 print();
Copy the code

You can define a variable to receive a function return value

    var res = print("I'm so excited to start learning functions today.");
    console.log(res);
Copy the code

B. Function declaration in advance:

    print1("hello");
    function print1(arg) {
         console.log(arg);
     }
Copy the code

The above code is in the correct position at execution time

    function print1(arg){
      console.log(arg);
    }
    
    print1("hello");
Copy the code

The variable will precede the function

     add();
     var num=1;
     function add(){
         console.log(num);
     }
Copy the code

The true order of execution:

      var num;
      function add(){
          console.log(num);
      }
      add();
      num=1;
Copy the code

C. Scope of the function:

Js prior to ES6 had no block-level scope only global scope and local scope

Global scope

Global scope: Variables defined outside a function (can be used anywhere) and variables defined inside a function are called local variables

When you call a function, the internal variables will be used first and if you don’t find global variables, the internal variables will be declared earlier

    var num = 1;
    function add(){
        var num=2;
        console.log(num);
        num++;
        return num;      
    }
    console.log(add());
    console.log(num);      

// var num ;
// function add(){
       
// num=2;
// console.log(num);
// num++;
// return num;
// var num;
/ /}
// num=1;
// console.log(add());
// console.log(num);
Copy the code

D. try to catch the error

In order to avoid errors in the program and cause the entire program crash we can use error capture

         num++;
            console.log("Can I do it?");

        // try to execute code that might have an error
        try {
             var num=1;
           num++;
            // catch catches when an error occurs in the code in the try
        } catch (error) {
            console.log(error);
        }finally{// The code in finally is executed whether the code in try is correct or wrong
             console.log("finally")}console.log("Can I do it?");
        function add(num1,num2){
            try {
                var res = num1+num2;
               
            } catch(err){
            }
             return res;
        }
Copy the code

/

E.【arguments 】

Is built into the system specifically to store the parameters passed by the function

function add() {
        console.log(arguments);
        // We can get the argument passed by the index number

        // console.log(arguments[0],arguments[1]);

        var sum = 0;
    // arguments.length Specifies the number of arguments
        for(var i =0; i<arguments.length; i++){ sum+=arguments[i];
        }
        return sum;

    }

    console.log(add(1.2.3.4.5.6.7.8.9));
Copy the code

F. Recursive functions (calls between functions)

If the function returns the function itself we call it a recursive function and it’s important to avoid an infinite loop and to have an explicit exit condition

What is the 20th place of Fibonacci number

    // 1 1 2 3 5 8 13
    // The number of gold in the NTH place = the number of gold in the n-1 place + the number of gold in the n-2 place

    function goldNum (n){   
        if(n==1||n==2) {return 1;
        }
        return  goldNum(n-1)+goldNum(n-2);
    }
    /* g(5) g(4 ) + g(3) g(3) +g(2); g(2)+g(1) =1+1; g(2)+g(1)+1 = 3 + 2 */ 
    // console.time("timer");
    // console.log(goldNum(40));
    // console.timeEnd("timer");
Copy the code

G. Functional expressions

Define a variable and assign a function to that variable

    console.log(add); // Add will be declared earlier
    var add = function (a,b){
        return a+b;
    }
   
console.log(add(2.3)) 

Copy the code

(Function is very important, in the later actual development process, almost all need it, now lay a good foundation, less regret in the future!!)