Functions

A function is a block of reusable code which can be called anywhere in your program once declared. Functions reduce code redundancy and make the code more modular and readable by allowing the developer to divide a big program into a number of small and manageable functions.
 JavaScript functions are defined with the function keyword. You can use a function declaration or a function expression.
 

Function Declarations

Earlier in this tutorial, you learned that functions are declared with the following syntax:

function functionName(parameters) {
  code to be executed
}

Declared functions are not executed immediately. They are "saved for later use", and will be executed later, when they are invoked (called upon).
 

Example:

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
function myFunction(a, b) {
    return a * b;
}
document.getElementById("demo").innerHTML = myFunction(4, 3);
</script>
</body>
</html>

Output:

12

Calling a Function

To invoke a function somewhere later in the script, you would simply need to write the name of that function as shown in the following code.
 

Example:

<html>
   <head>
   
      <script type="text/javascript">
         function sayHello()
         {
            document.write ("Hello there!");
         }
      </script>      
   </head>
   <body>
      <p>Click the following button to call the function</p>      
      <form>
         <input type="button" onclick="sayHello()" value="Say Hello">
      </form>      
      <p>Use different text in write method and then try...</p>
   </body>
</html>

Output:

Click the following button to call the function
say hello //button
Use different parameters inside the function and then try...

Hello there.

Try

Function Parameters

Till now, we have seen functions without parameters. But there is a facility to pass different parameters while calling a function. These passed parameters can be captured inside the function and any manipulation can be done over those parameters. A function can take multiple parameters separated by comma.

Parameter Rule:

  • JavaScript function definitions do not specify data types for parameters.
  • JavaScript functions do not perform type checking on the passed arguments.
  • JavaScript functions do not check the number of arguments received.
     

Example

<html>
   <head>
   
      <script type="text/javascript">
         function sayHello(name, age)
         {
            document.write (name + " is " + age + " years old.");
         }
      </script>      
   </head>
   <body>
      <p>Click the following button to call the function</p>      
      <form>
         <input type="button" onclick="sayHello('Zara', 7)" value="Say Hello">
      </form>      
      <p>Use different parameters inside the function and then try...</p>
   </body>
</html>

Example:

Click the following button to call the function
sayHello //button
Use different parameters inside the function and then try...

Zara is 7 years old.

 

Try

Scope of Functions

All the functions in javascript have a global scope.

Nested Functions:

Simply put, a function is said to be a nested function when it is declared within another function.

Note: Every function in javascript has access to the scope above itself.