MSc-IT Study Material
June 2010 Edition

Computer Science Department, University of Cape Town

Creating user-defined functions

A simple function to display the String "hello"

Lets assume that we want to create a function that simply executes the following line:

    document.write( "Hello" );

This function does not need any arguments, since it will do the same thing every time. The body of our function will be the single JavaScript statement above.

The table below illustrates a design for our function:

Table 16.1. Function to display "Hello"

Name (optional) 
Arguments (optional) 
bodydocument.write("Hello")
Returns (optional) 


Depending on how we create the function, we may or may not need to name the function (in most cases it is useful to name functions).

Creating a function using function statements

The most convenient way to define a function is to use the function operator. The following example defines the displayHello function previously used:

	function displayHello()
	{
	document.write( "Hello" );
	}
      

The function operator requires the following:

	function displayHello()         -- Function name followed by list of arguments between ( )
	{                                           -- Open Brace
	document.write( "Hello" );   -- Sequence of JavaScript statements forming the function body
	}                                           -- Close Brace
      

As can be seen above, if there are no arguments an empty pair of parentheses () is written after the function name.

Our function design looks like the following:

Table 16.2. Function to display "Hello"

Name (optional)displayHello
Arguments (optional) 
bodydocument.write("Hello")
Returns (optional) 


Creating a function using the 'Function()' constructor

Another way to define a function is by using the Function() constructor. Recall that functions are themselves just objects, and that objects are created using constructor functions. Function() allows a function to be defined by passing a series of Strings to it. All but the last String lists the arguments to the function, while the last String defines the sequence of statements that form the function's body.

The Function() constructor returns a function which can be assigned to a variable of your choice.

So, we can now define displayHello() in this alternate way:

	var displayHello = new Function( "document.write( 'Hello' );" );
      

Notice how there is no need for braces { } to be used, since the body statements are contained in a String. Defining functions with Function() constructor is less convenient than using the function operator, but is extremely useful when dynamically creating functions, since the function arguments and body can easily be created as Strings.

Notice the single quotes around 'Hello' — a double quoted String cannot appear inside a double quoted String. Having single quotes on the outside, and a double quoted "Hello" works just as well:

	var displayHello = new Function( 'document.write( "Hello" );' );
      

In our above call to Function() there is only one String, since displayHello() requires no arguments.

As before, our function design looks like this:

Table 16.3. Function to display "Hello"

Name (optional)displayHello
Arguments (optional) 
bodydocument.write("Hello")
Returns (optional) 


Creating a function using function literals

Another third way to define a function is by using a function literal. This approach is very similar to using the function operator, except that the function is now nameless, although it can still be assigned to an arbitrary variable.

To define displayHello using a function literal, we would write the following:

	var displayHello = function() { document.write( "Hello" ); }
      

Notice that function literals use a very similar syntax to function statements, and differ only in that the name of the function is not given.

Note

Although there are three different ways to define a function, in most situations you will find that using named functions defined with the function operator (the first technique described above) is easiest, although the other techniques all have their uses.

All the examples in the rest of this unit (and in most of the other units) define functions using the function operator.