Function Calls / Invocation Expressions

By: Dusty Arlia
Published on November 8, 2012
Last Updated on Thursday, July 09, 2015 at 11:43 PM
Total Updates: 7

When reading books or tutorials on programming languages, you might come across the term call; typically used in the phrase "call the function". "Calling" a function simply means to run the function. You do this by typing the function's name/identifier followed by a pair of parentheses. So if you wanted to run the function nextPicture you would write it like this nextPicture().

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. For 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);

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. This way your array can hold the multiple values your function needs.

If your function doesn't accept arguments, call the function with empty parentheses like this:


The parentheses are a required part of a function call, even if you don't have any arguments to pass. Leaving out the parenthesis will likely return the entire text of the function.