Functions Passed as Arguments

Contact Us or call 1-877-932-8228
Functions Passed as Arguments

Functions Passed as Arguments

In JavaScript, functions are first-class objects. Functions aren't just an immutable block of code that can only be invoked. Rather, each function we declare becomes an object, with its own properties and methods, that can be passed around like any other object.

Let's see how we can use functions as parameters to other functions. Consider the following example:

Code Sample:

---- C O D E   O M I T T E D ----

	//let's create an array with 5 values
	var values = [5, 2, 11, -7, 1];
	//this function simply adds the values of 'a' and 'b' and returns the sum
	function add(a, b) {
		return a+b;
	//this function simply multiplies the value of 'a' times 'b' and returns the result		
	function multiply(a, b) {
		return a*b;
	/* the 1st param passed to it is the numbers (in this case, the array of values)
	 * the 2nd param passed to it is the initial Value (could be 0, 1, or whatever)
	 * the 3rd param passed to it is the operation - which will actually be one of the functions declared above (add or subtract)
	function combineAll(nums, initialValue, operation) {
		//let's initialize the local variable 'runningResult' with the initialValue passed to it
		var runningResult = initialValue;
		//loop the nums array
		for (var i=0; i < nums.length; i++) {
			/* for each iteration, call the function (which can be add or multiply) passing the 2 params
			 * on the first iteration, runningResult will be the initialValue; after that, it will be the value returned from operation(runningResult, nums[i])
			 * nums[i] simply refers to the current number in the iteration; the 1st will be 5, then 2, and so on.
			runningResult = operation(runningResult, nums[i]); 
		return runningResult;
	//notice the 3rd param passes the "add" function - you can pass a function into anoter function!
	var sum = combineAll(values, 0, add);
	//notice the 3rd param passes the "multiply" function - you can pass a function into anoter function!
	var product = combineAll(values, 1, multiply);
	alert("Sum: " + sum); //should be 12: 5+2+11+(-7)+1
	alert("Product: " + product); //should be -770: 5x2x11x-7x1 
---- C O D E   O M I T T E D ----

You may be wondering what the following function call means: var sum = combineAll(values, 0, add);. In this statement we are passing the function add as the third parameter of combineAll. We are not invokingadd yet, just passing a reference to it. Note that the open and close parenthesis aren't used after add. That should serve as a tipoff that this is not a function invocation.

The line that ultimately invokes add is:

runningResult = operation(runningResult, nums[i]);

...which received a reference to add in the operation parameter. When operation is invoked, in reality, it is add that is getting called, returning the sum of the two values passed in.

This is a very important technique and the combineAll function is often called reduce. Take your time to review the code and run the example until you feel comfortable with it. We will be using this capability extensively in the remaining lessons.