What is a function

Function (nickname: method) : Blocks of code that implement the same function

2. Classification of functions

  1. System functions (functions in mathematical objects, such as generating random number function random(), rounding up/down function ceil()/floor(), rounding up function round(), solving power function pow(), etc.). System functions are functions defined by JS, which can be called directly.
  2. User-defined functions refer to functions that users can declare themselves and implement the expected processing functions.

Three, function declaration, call

Function function name (){function body} or function function name (argument 1, argument 2, argument 3, argument….) {function body}

<script>
    // Declare ordinary functions
    function sum(){   
        console.log("I was used.");
    }
    // Declare a function that takes only one argument
    function sum1(a){
        console.log(a);
    }
    // Declare a function with multiple arguments
    function sum2(a,b){
        console.log(a+b);
    }
</script>
Copy the code

Call syntax: function name (); Or function name (parameter 1, parameter 2, parameter 3, parameter….) ;

<script>
    // Declare ordinary functions
    function sum(){   
        console.log("I was used.");
    }
    // Declare a function that takes only one argument
    function sum1(a){
        console.log(a);
    }
    // Declare a function with multiple arguments
    function sum2(a,b){
        console.log(a+b);
    }
    
    // Call a normal function
    sum();
    // Call a function with one argument
    sum1(25);
    // Call a function with multiple arguments
    sum2(25.12);
</script>
Copy the code

After the function is declared, it must be called before it can be executed. If we compare the function to a washing machine, this function can complete the washing function. However, if there is no manual operation, the washing machine cannot be started and the clothes cannot be washed. So once a function is defined, it must be called before it can be executed.

4. Parameters of a function

4.1 classification of function parameters

  1. Formal parameters: Arguments written inside the parentheses of the function name when declaring a function

  2. Actual arguments: Arguments written inside the parentheses of the function name when a function is called

<script>
    // Declare the function
    function sum(a,b){   //a,b are the parameters
        console.log(a+b);
    }
    
    // Call the function
    sum(25.30);// 25,30 are arguments
</script>
Copy the code

Parameter passing: Function calls pass values to function parameters via arguments, which then pass values inside the function. The passing direction is the passing of the argument to the parameter. (Note that parameters cannot pass values to arguments).

Functions of function Encapsulation & Function encapsulation

Function encapsulation improves code reuse and program maintainability

  1. Common implementation functions
  2. Put it in the shell of function
  3. Take a name
  4. Consider parameters – parameters are optional and can be multiple
  5. Call a function
  6. Write a function instruction manual (comment)
    1. Function description
    2. Description of function parameters

The return value of the function

After a function is wrapped, the result of processing can only be used inside the function without any operation, that is, the result of the function processing is a local variable without any operation. So how do you get the result of a function processing outside of a function?

  1. You can assign the result of the function processing to the global variable by using the global variable, and then use the (Disadvantages, multiple functions, it is necessary to declare multiple global variables, easy to cause memory waste; If there is only one or fewer global variables, variable pollution may occur.
  2. throughreturnThrowing the value (back to where the function was called) benefits: No memory waste.

6.1 Two Uses of Return

  1. Returns the result of the function processing
  2. Terminating function (similar to break. Break can only be used in a statement, return can only be used in a function.

Return Program data flow

Return after console.log()