Arguments

By: Dusty Arlia
Published on Friday, April 19, 2013, 09:20 AM
Last Updated on Saturday, July 11, 2015 at 6:41 PM
Total Updates: 5

An argument is a literal value or a variable that gets sent to the function as input. When calling a function, sometimes you may want to pass an argument(s) to that function. You may include multiple arguments or none at all, they are optional. Arguments get placed between the parentheses of the function call. Here is an example:

//"function bake(baking_temp, duration)" is the function declaration
//"bake(350, 20)" is the function call

function bake(baking_temp, duration) {
//function body
}

bake(350, 20);   

In the function call above, 350 stands for the temperature and 20 stands for minutes. 350 and 20 are both arguments that get passed to the bake() function. baking_temp and duration are the parameters that receive the argument values.

When you call a function (or invoke a function), you can pass arguments to it. Arguments are the variables or literal values that get placed in between the parentheses after the function name. The order and data type of the arguments matter. The values of the arguments get assigned to the parameters created when the function was declared. Here's another example:

//"function add_numbers(number1, number2)" is the function declaration
//"add_number(2, 3)" is the function call

function add_numbers(number1, number2) {
    var answer = number1 + number2;
    return answer;
}

var solution = add_numbers(2, 3);
alert(solution);

In this example, we pass the arguments 2 and 3 to the parameters number1 and number2. The function calculates the answer and returns its value back to the function call. The function call is also referred to as the invocation expression. The invocation expression is said to evaluate to 5.

You can include any number of parameters in your function declaration and you can pass any number of arguments to that function. Here is the syntax for a function with multiple parameters:

function functionName(parameter1, parameter2, parameter3) {
//Your JavaScript function code 
}

And this is how you call your function with an equal amount of arguments, in the same order:

functionName(argument1, argument2, argument3);

When you call this function argument1 is stored in parameter1, argument2 in parameter2, and argument3 in parameter3.

If you are passing variables as your arguments, you might notice that their names might not be the same as the parameter names. You are allowed to have parameters with the same names as the arguments, but this opens up the small chance that you can have a name collision. It is recommended that you use different names for your parameters.

The JavaScript interpreter doesn't generate any errors if the function declaration has more parameters than arguments you pass. Chances are that you will get unexpected results, because the function might be expecting one of those arguments.

Likewise, if you pass more arguments than available parameters, the JavaScript interpreter will silently ignore the extra arguments (extra arguments are still accessible from the arguments array). It's common practice that you should specify enough parameters in your function declaration to receive the amount of arguments you plan on passing. This makes your code easier to read and maintain. If your function receives an arbitrary number of arguments, pass an array as a single argument instead.

Comments:

Ad: