JavaScript – Exercise 14 – Functions

Advertisements
Advertisements

JavaScript functions are named block of codes that can be called several times. It save time by programmers not writing the same code several times. Since, it is widely used feature, we decided to introduce it in object example section and not earlier.

In this article, we are going to understand a function and its types. Also, you will write an example program with the help of functions.

Function types

There are several types of functions in JavaScript. Since, JavaScript is not a strictly typed language, the functions in JavaScript are different.

  • Function without arguments
  • Function with arguments
  • Function with return
  • Function with no return

Function without arguments

Function without arguments are those functions that does not take any arguments. The function can compute using local or global variables and may be return or not return anything.

The structure of such a function is given below:

Advertisements
function functionName () {

    statements;           //function can execute some statements 
    return variableName; //optionally return something
}

Function with arguments

Another kind of function is function with arguments. You have already seen an example of such a function – the array push() function. The push() function takes elements and push them to the array.

The structure of function with arguments is given below.

function functionName ( arg1, arg2,...,arg n) {

    arg3 = arg1 + arg2; //execute some statements using arguments
   return arg3;         //optionally return values
}

Function with return

Though it is not compulsory for JavaScript functions to return any value. However, some functions return a value after computation. You can output this value or use them for further computation.

The structure of function with return is given below:

function add(number1, number2) {
    return number1 + number2;
}

We can assign the returned value to another variable for further calculations. For example.

var total = add(number1, number2); //calling add() with arguments

Function with no return

A function with no return will compute the result and directly output it to HTML or the web console.

Typical structure of such a function is as follows:

function print-output () {

     document.write("Hello World");
}

Example Program: JavaScript Functions

In this example, we are going to evaluate an expression using functions. You must use different types of function while doing the exercises.

  • Write a function to compute square of a number.
  • Write a function to evaluate an expression.
  • Print the results.

Program Code:

<!DOCTYPE html>
<html>
<head>
  <title>JavaScript - Functions</title>
  <meta charset = "utf-8">
 </head>
 <body>
      <h1 id="result">Get output now</h1>
      <script>
          /* Variable declarations */
          var square;
          var number;
          /* Find square of a number */
          function varSquare(number){

                    return number * number;
           }
  
       /* assign the returned value to variable square */
       square = varSquare(25);
       /* print the output */

       /* evaluate expression */
       function evalExpression(square){
                    var output = square + 100;
                    return output;
       }
       var result = evalExpression(square);
       document.getElementById("result").innerHTML = 
       "Result=" + " " + result;

       </script>
 </body>
 </html>

Output – JavaScript Functions

Output - JavaScript - Functions
Output – JavaScript – Functions
Advertisements
Advertisements
Advertisements