Functions, arguments and function object

Function – A function is a block of code that has a name. Whenever that name is used the function is called, which means that the code within that function is executed. Functions may also be called with values, known as parameters, which may be used inside the body of the function. Functions serve two purposes. A function is an organizational tool, in the sense that it permits to perform the same operation without simply copying the same code.

The second purpose of JavaScript functions is to link actions on a Web page with JavaScript code. Mouse clicks, button presses, text selections, and other user actions can call JavaScript functions by including suitable tags in the HTML source for the page.

The syntax for a function statement in JavaScript is as follows:

function Name ( listofparams ) {

body

}

The function’s Name is given immediately after the function keyword. All function names should be unique, and also should not conflict with any of the statement names which JavaScript itself uses (known as the reserved words). Web page cannot have a function named while, for example, and two functions should not have be named Help.

Argument – The listofparams is a comma-separated list of the values that are passed into the function. These are referred to as the function’s parameters, or arguments. This list may be empty, indicating that the function does not use any arguments (often called a void function).

The function’s body is the set of statements that make up the function. An example listed below shows a function that adds up all the integers starting at 1 and ending at a value given as the sole argument.

function summation ( endval ) {

var thesum = 0;               // this variable will hold the sum

for ( var iter = 1; iter < endval; iter++ ) {

thesum += iter;         // add the integer into the sum

}                         // end of the for loop

return( thesum );         // return the sum

}

When the function is called, as summation(14) for example, the actual value 14 is used for endval within the function. The function then executes the for statement, with iter < 14 as its termination condition, adding in each successive value into the variable thesum. When the for loop is done, the function executes the return statement. This causes the function to give the value inside the return statement back to the caller. This means that if we write

var sum14;

sum14 = summation(14);

The variable sum14 is set to the value returned by the summation function when endval is given the value 14, namely 105. Functions can return any type of value, and are not restricted to returning integers.

Function Object – The predefined Function object specifies a string of JavaScript code to be compiled as a function.

Create a Function object:

functionObjectName = new Function ([arg1, arg2, … argn], functionBody)

  • functionObjectName is the name of a variable or a property of an existing object. It can also be an object followed by a lowercase event handler name, such as window.onerror.
  • arg1, arg2, … argn are arguments to be used by the function as formal argument names. Each must be a string that corresponds to a valid JavaScript identifier; for example “x” or “theForm”.
  • functionBody is a string specifying the JavaScript code to be compiled as the function body.

Function objects are evaluated each time they are used. This is less efficient than declaring a function and calling it within your code, because declared functions are compiled. The following code assigns a function to the variable setBGColor. This function sets the current document’s background color.

var setBGColor = new Function(“document.bgColor=’antiquewhite'”)

To call the Function object, you can specify the variable name as if it were a function. The following code executes the function specified by the setBGColor variable:

var colorChoice=”antiquewhite”

if (colorChoice==”antiquewhite”) {setBGColor()}

Back to Tutorial

Get industry recognized certification – Contact us

Menu