javaScript functions

javascript functions can be used to create script fragments that can be used over and over again. a javascript function are used to encapsulate code that will be executed when 'someone' calls it

look this code

    <!doctype  html>
    
        <html>
        
           <head>
             
                 <script>
                 
                       function   showinformation(){
                       
                               document.write("firstname___"+"javascript");
                               
                               document.write('<br/>'+"lastname____"+"function");
                               
                               document.write('<br/>'+"age______"+24);
                               
                               document.write('<br/>'+"salary____"+10000);
                         
                              }
                              
                 </script>
               
                  </head>
                  
                     <body>
                       
                          <h1>user information </h1>
                          
                          <button  onclick="showinformation()">show</button>
                          
                       <body></html>

syntax

javascript functions are declared with the function keyword, operation code inside curly {} braces.

    function         functionname()
                                            {
                                                 // function code block
                                                                                          
                                            }
    or      

    function         functionname()
                                            {   code part 
                                                                                          
                                                 return  statement;   //this is optional statement
                                            }                                                                 
 

a javascript function is executed when a call to that function is made anywhere within the script, the page, an external page, or by an event.

functions are always guaranteed to return some value when executed.

note:forward references to functions are generally not allowed; in other word, you should always define a function before calling it.


javaScript functions with arguments

you pass information to a function by enclosing the information in parentheses after the name of the function.pieces of information that are passed to a function are called arguments or parameters.

calling method syntax:

                        showinformation(arguments1,arguments2,arguments3)

when you declare the function, declare the argument

     function        showinformation(var1,var2,var3){  code part  }

the variables and the arguments must be in the expected order.the first variable is given the value of the first passed argument etc:

              <script>
                 
                       function   showinformation(name,lastname,age,salary){
                       
                               document.write("firstname_______"+name+"<br/>");
                               
                               document.write("lastname________"+lastname+"<br/>");
                               
                               document.write("age_________"+age+"<br/>");
                               
                               document.write("salary________"+salary+"<br/>");
                         
                              }
                              
                 </script>
               
<button  onclick="showinformation('vinod','kumar',25,100000)">show
</button>

javaScript function with return statement

when using the return statement, the function will stop executing, and return the specified value.

              <script>
                 
                       function   sum(a,b,c){
                       
                                                      return  (a+b+c);
                                                    }
                              
                 </script>
               
<button  onclick="alert('sum ='+sum(20,40,50))">show sum</button>

recursive functions

a javascript recursive function is one that calls itself.

    function  factorial(n){  if( n==0 ){ return 1 ; } else{ return n * factorial( n-1 ); } }

local variables

a variable declared within a javascript function becomes local and can only be accessed from within that function.(the variable has local scope).

local variable is one limited to the particular block of code it is defined within.


global variables

variables declared outside a function, become global, and all scripts and functions on the web page can access it.


the scope of javascript variables

the lifetime javascript variables starts when they are declared.

global variables are removed from memory when you close the page.

local variables are removed from memory when the function execution is completed.

  <script>
        
      var     value  = 30;
            
         function   show(){
                
                                   var   name ="variable scope";
                     
                                   document.write("name__"+name+"<br/>"+value);
                        
        var   top  = "this is javascript local scope variable";//define a local variable.   

                                   document.write("<br/>"+"top___"+top);
                                 }
                         
         show();
               
         document.write("<br/>"+"value_____"+value); 
               
     </script>

the javascript functions can be constructed in three main ways.we begin with three 'hello, world!' examples:

              way1

function      hello()
                 
     {
       
   	  alert("hello, world!");
                
     }
              way2

 var  hello = function()
                 
   {       
       alert("hello, world!");
                
   }
   
     anonymous function
              way3

var hello = function(      
         
         
  'alert("hello, world!");' 
     
         ); 
              
       function as object
    

each function:

  • can be called with hello()
  • does not expect any arguments
  • performs an action to alert the user with a message
  • undefined is returned when execution is finished