Page 208 - Web Applications (803) Class 12
P. 208

The return Statement
              Frequently, functions calculate a return value. The caller is “returned” the return value. If a statement called
              the function, JavaScript control will “return” to run the statements that come after the function invoking
              statement. A return statement in JavaScript causes the function to terminate.

              Example:

                  <html>
                  <body>
                  <h2>JavaScript Functions</h2>
                  <p>Function to convert temperature from Fahrenheit to Celsius</p>
                  <script>
                  function Convert_to_Celsius(f) {

                    return (5/9) * (f-32);
                  }
                  result=Convert_to_Celsius(76.5);
                  document.write("The Celsius temperature is "+result);
                  </script>
                  </body> </html>
              Output of the preceding program is as follows:













              Practice Question  1:

                  <html>

                  <body>
                  <h2>JavaScript Functions</h2>
                  <p>Function to display the greatest of three numbers</p>
                  <script>
                  function gr_num(p,q,r) {
                    if (p>q && p>r)

                       return p;
                    else if (q>p && q>r)
                     return q;
                    else
                     return r;

                  }
                  result=gr_num(56,78,23);  //Assuming all 3 numbers are different



                206   Touchpad Web Applications-XII
   203   204   205   206   207   208   209   210   211   212   213