JS functions --- Introduction --- basic grammar, definition, function parameters, return values

  • Function: the code repeat cook package, can be directly invoked when needed
  • The action function: code reuse
  • Function need to define before you can use
  • Function name: hump to follow nomenclature
  • Once the function of the same name, the function will cover the front of the back
  • Ctrl + left mouse button ----> Go To Definition
  • A function is a function best

 

 

Function parameters:

  1. When the function definition, after the function name in parentheses is the variable parameters, the purpose is to function in the time of the call, the user-entered the value of the operation
  2. At this time behind the function definition parentheses variables called parameters; wrote two variables, there are two parameters,
  3. When a function call, follow the prompts way to assign values ​​to variables ---> called by value, this value is passed to the variable (parameter)
  4. Parameter: function variable in time parenthesis defined in the parameter called
  5. Arguments: function called when the value passed in parentheses is called the argument, the argument can be a variable may be a value

 

    // function definition 
    function consoleSum (X, Y) {
       var SUM = X + Y; // Calculation ---- function and 
      the console.log (SUM); // output and a second function --- 
    }
     // Function call 
   var num1 = the parseInt (prompt ( "enter first number" ));
    var num2 = the parseInt (prompt ( "enter the second number" )); 
   consoleSum (num1, num2);

 

Function's return value:

  1.     set: Set
  2.     get: Obtains
  3.     Return value: return keywords have internal functions, and the content in the back keywords, the content is returned
  4.     When the function is called, it requires the return value, then the receiver on the definition of variables, you can

 

   function GetSum (X, Y) {
      var SUM = X + Y;
      return SUM; // put and return 

   } 
   // function call 
   var Result = GetSum (10, 20 is ); 
   the console.log (Result +10);

 

  • If there is a return function, the function returns the value there
  • If a function does not return, then this function does not return value
  • If there is no clear return value of a function, so when the call is received, the result is undefined
  • (No explicit return value: function does not return, there is the function return, but later return nothing)
  • Function does not return a value, but at the time of the call is received, then the result is undefined
  • Variable declaration, there is no assignment, the result is undefined
  • If a function has parameters, there are parameters of function
  • If a function takes no arguments, no arguments
  • The number and the number of arguments can be inconsistent parameter
  • return the following code will not be implemented

 

        // there are parameters, the function returns a value 
         function F1 (X, Y) {
            return X + Y; 
         } 
         // have parameters, no return value of the function 
         function F2 (X) { 
           the console.log (X); 
         } 
         // No parameter, the function returns a value of 
         function F3 () {
            return 100 ; 
         } 
         // no return value of the function parameter no 
         function F4 () { 
           the console.log ( "Sava Di" ); 
         }

 

Guess you like

Origin www.cnblogs.com/jane-panyiyun/p/11934695.html