JavaScript Function - function with parameters, with parameter values returned

With function parameters 

  speak basic usage function before, let's look at the function parameters. 

  In function, the parameters can be infinite, with between functions "," spaced apart like ~ 

  

  <Script>
     function Demo (m, n-) {            // Parameters m, n- 
        var SUM = m + n-; 
        Alert (SUM); 
    } 
    Demo ( 2,3);          // pass the parameter 2 = m, =. 3 n- 
    Demo (20,30);        // pass = 20 is the parameter m, 30 = n- 
    Demo (200,300);      // pass parameters m = 200, n 300 = 
  </ Script>   order to be consistent when the transfer note 
  output as follows: 
  in addition to data transfer, we can also impart information via the function parameters. For example: <Script>
     function Demo (Age, Color) { 
        Alert ( "I am" + age + "," + " my favorite color is" +




  color); 
    }
   </ Script> 
  <Button οnclick = "Demo (18 is, 'blue')"> to </ button> // create a button, where the function call, and transmission parameters of age and color   output as follows: 
  function return value of 
  what is the function that returns a value with it? 
  That is, the value returned to the place call it, achieved with the return to return 
  to note is: once the use of return, the function stops execution, while the value is returned to the place that called it <Script>
     function Demo () {
         return ( "Look , return directly " ); 
    } var a = Demo () +", the first return " ;
     var B = Demo () +", the first two return " ; 
    Alert (a); 
    Alert (B); </ script>
  Output is as follows: 
  At this point, if coupled with a return behind the alert (), then the alert () is not going to pop up, because after the return function to stop the execution. If you want to pop up before the return can only be used 
  to look at how to use the functions in the HTML with a return value











    
  





  <p id = "yuansu"> in the P-tag may be displayed later comparison result m, n size </ P> 
  <Script> function Demo (m, n) {
         IF (m> n-) {
             return ( " m is greater than n-" ); 
        } the else IF (m < n-) {
             return (" m less than n-" ); 
        } the else IF (m = n-) {
             return (" m is equal to n-" ); 
        } 
    } var I = Demo (. 8 , 10 ); 
    document.getElementById ( . "yuansu") the innerHTML = I;
   </ Script> 
  output follows: before unexecuted <p>Label   
                    after execution
      
    <p> tag   


  before the presentation, you can see that we typically define a number of variables, but in different locations defined variables, the use of different. E.g.


   <Script>
     var m =. 1; = n-2; // global variables, can be used anywhere 
    function Demo () {
         var I =. 5; // local variables, only this function can be used in 
        X =. 3; / / global variables, previously invoked under this function can be used 
    } 
    demo (); 
    Alert (m); // m, n-output can; I can not be output; x must be called a case of a function to output the demo 
  </ script> 
  as described above, m, n, x, are global variables; I as a local variable. 
 ---------------- 
Disclaimer: This article is CSDN blogger "embarrassing baby advanced programmer in mind." Original article, follow the CC 4.0 by- SA copyright agreements, please attach a reprint the original source link and this statement. 
Original link: HTTPS: // blog.csdn.net/jiongyixuan/article/details/50274597

 

Guess you like

Origin www.cnblogs.com/turnip/p/11416086.html