Recipe 1.9 Generalizing a Function to Enhance Reusability

1.9.1 Problem

You want to perform slight variations of an action without having to duplicate multiple lines of code to accommodate the minor differences.

1.9.2 Solution

Add parameters to your function to make it flexible enough to perform slightly different actions when it is invoked rather than performing exactly the same action or producing the same result each time.

Define the parameters that account for the variability in what you want the function to do:

function myParamsFunction (param1, param2, param3) {
  trace("The average is " + (param1 + param2 + param3)/3);

If you don't know the exact number of parameters the function will receive, use the built-in arguments array to handle a variable number of parameters.

1.9.3 Discussion

A function that doesn't accept parameters generally produces the same result each time it is invoked. But you often need to perform almost exactly the same actions as an existing function, but with minor variations. Duplicating the entire function and then making minor changes to the second version is a bad idea in most cases. Usually, it makes your code harder to maintain and understand. More importantly, you'll usually find that you need not only two variations but many variations of the function. It can be a nightmare to maintain five or six variations of what should ideally be wrapped into a single function. The trick is to create a single function that can accept different values to operate on.

For example, if you have an average( ) function, you want to specify arbitrary values to be averaged each time it is invoked, instead of having it always average the same two numbers. You can accomplish this goal using parameters.

The most common way to work with parameters is to list them within the parentheses in the function declaration. The parameter names should be separated by commas, and when you invoke the function you should pass it a comma-delimited list of arguments that correspond to the parameters it expects.

The terms "parameters" and "arguments" are often used interchangeably to refer to the variables defined in the function declaration or the values that are passed to a function when it is invoked.

Here is a simple example of a function declaration using parameters and a function invocation in which arguments are passed during the function call:

// Define the function such that it expects two parameters: a and b.
function average (a, b) {
  trace("The average is " + (a + b)/2);

// When you invoke the function, pass it two arguments, such as 6 and 12, that 
// correspond to the a and b parameters. 
// This call to average(  ) displays: "The average is 9"
average(6, 12);

Parameters work in exactly the same way with function literals as they do with named functions:

average = function (a, b) {
  trace("The average is: " + (a + b)/2);

In most situations it is best to declare the parameters that the function should expect. However, there are some scenarios in which the number of parameters is unknown. For example, if you want the average( ) function to average any number of values, you can use the built-in arguments array, which is available within any function's body. All the parameters that are passed to a function are automatically placed into that function's arguments array.

// There is no need to specify the parameters 
// to accept when using the arguments array.
function average (  ) {
  var result = 0;

  // Loop through each of the elements of the arguments array 
  // and add that value to result.
  for (var i = 0; i < arguments.length; i++) {
    result += arguments[i];
  // Then divide by the total number of arguments.
  trace("The average is " + result/arguments.length);

// You can invoke average(  ) with any number of parameters. 
// In this case, the function will display: "The average is 7.5".
average (3, 6, 9, 12);

Technically, arguments is an object with additional properties beyond that of a basic array. However, while arguments is a special kind of array, you can still work with it in the same ways that you would a regular array.

    Part I: Local Recipes
    Part II: Remote Recipes