Open In App

How to create a function that invokes each provided function with the arguments it receives using JavaScript ?

Last Updated : 02 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to create a function that invokes each provided function with the arguments it receives using JavaScript.

It helps in reducing the effort of creating the same instance of code again and again. In this article let us see how to create a function that invokes each provided function with the arguments it receives and returns a value in JavaScript.

Functions can be defined in two ways:

Syntax:

function <function_name>(arg_1,arg_2,...)
{
   <set of instructions>;
}

//INVOKER
var returned_value= function_name(arguments);

OR 

var function_name = function(arg_1,arg_2,...)
{
  <set of instructions>;
}

// INVOKER
var returned_value = function_name(arguments);

Below are the examples that help us to understand the problem easily.

Example 1: Function to separate even and odd numbers in an array. The findEven and findOdd functions take the same arguments by the segregateEvenodd function and are invoked in the segregateEvenodd function.

Javascript




<script>
    // Defined array
    var ar = 
        [1, 2, 2, 3, 4, 5, 6, 6, 7, 8, 8, 8];
    function findEven(ar){
      var res1 = [];
      for (let geek = 0; geek < ar.length; geek++) {
          if (ar[geek] % 2 === 0) {
              res1.push(ar[geek]);
          }
      }
      return res1;
    }
      
    function findOdd(ar){
      var res2 = [];
      for (let geek = 0; geek < ar.length; geek++) {
          if (ar[geek] % 2 === 1) {
              res2.push(ar[geek]);
          }
      }
      return res2;
    }
      
    function segregateEvenOdd(ar) {
      
      // Invoking findEven and findOdd functions
      var even = findEven(ar);
      var odd = findOdd(ar);
      console.log("Before Segregation: ");
      console.log(ar);
      console.log("After Segregation: ");
      console.log("Even integers: " + even);
      console.log("Odd integers: " + odd);
    }
      
    // Invoker
    segregateEvenOdd(ar);
</script>


Output:

"Before Segregation: "
[1, 2, 2, 3, 4, 5, 6, 6, 7, 8, 8, 8]
"After Segregation: "
"Even integers: 2,2,4,6,6,8,8,8"
"Odd integers: 1,3,5,7"

Example 2: Function to find minimum and maximum in an array. The findMin and findMax functions take the same arguments by the FindMinMax function and are invoked in the FindMinMax function.

Javascript




<script>
    // Defined array
    var ar = [20, 30, 40, 50, 60, -20, -40, 90, 100];
    function findMin(ar) {
        var res1 = Number.MAX_VALUE;;
        for (let geek = 0; geek < ar.length; geek++) {
            if (ar[geek] < res1) {
                res1 = ar[geek];
            }
        }
        return res1;
    }
    function findMax(ar) {
        var res2 = Number.MIN_VALUE;
        for (let geek = 0; geek < ar.length; geek++) {
            if (ar[geek] > res2) {
                res2 = ar[geek];
            }
        }
        return res2;
    }
      
    function FindMinMax(ar) {
        // Invoking findMin and findMax functions
        var min = findMin(ar);
        var max = findMax(ar);
        console.log("Given array : ");
        console.log(ar);
        console.log("Minimum in the array: " + min);
        console.log("Maximum in the array: " + max);
      
    }
    // Invoker
    FindMinMax(ar);
</script>


Output:

"Given array : "
[20, 30, 40, 50, 60, -20, -40, 90, 100]
"Minimum in the array: -40"
"Maximum in the array: 100"


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads