JAVASCRIPT

JAVASCRIPT FUNCTION

0  0 2016-08-02 07:49:29

JavaScript Function :-
A function is a group of statements block that perform some task. User can define number of functions in one application but each function syntax has unique. For executed a JavaScript function "something" invokes it (calls it).
JavaScript Function Syntax :-

function function_name (parameter1, parameter2)
{
    // The function performs operation on the above parameter
    var x=paramter1 * parameter2;             
    //And return the some result
    return x;
 }

Function Invocation in JavaScript :-

Function will execute when something invokes (calls) the function:
•    When some event occurs related function. (user clicks a button)
•     It is invoked when call by user written code line(when it called from JavaScript code).

Function Return :-
A return statement will give some values to calling statement it can be variable or innertag property. Return statement is last statement in the function body. When will reach at return statement function will stop executing .

Function Example :-
// Function Definition
function function_name(parameter1)
{
    return parameter1 * 10;
}
// Function Calling statement (Line)
document.getElementById("id1").innerHTML = function_name(7);

Function Definition :-
    In a JavaScript we can write function in 2 different places that work same. Only the difference in syntax of function that are as below.

1)In External File :-
We have to create one new file that is with .js extension in our application directory and have to call that file in head tag or in end of the HTML file in <Link> tag.

Example :-
I] File.html
   <!DOCTYPE html>
   <html>
           <head>
                  <script type="text/javascript" src="filename.js" ></script>
           </head>
              <body>
                      <p id="id1"></p>
              </body>
  </html>

II] filename.js
document.getElementById("id1").innerHTML = function_name(10,20);
Function function_name(parameter1, parameter2)
{
    Var x= parameter1 * parameter2;
    return x;
}    

2)In Same File :-
  We can place code of JavaScript in same file also using script tag. Put all JavaScript code in between the script tag.

Example :-
<!DOCTYPE html>
<html>
    <body>
        <p>This example calls a function of JavaScript:</p>
        <p id="id2"></p>

        <script>
            function function_name(f)
            {
                    return f*10;
            }
            document.getElementById("id2").innerHTML = function_name(7);
         </script>
    </body>
</html>

 

Comments (0)

Post Your Comments

Headquarter



Hong Kong Office



Find Us On

Website Development Company
SEO
Digital Marketing
website development company in Hong Kong
website designer in pune