Javascript Functions

Functions are an important part of any programming language. They enable you (the programmer) to create reusable chunks of code that perform specific tasks (e.g. validate a field in a form). In other programming languages functions may be referred to as subroutines or procedures. Javascript has a number of functions built into the language and it also gives you the ability to create and use your own.

Built In Functions

Javascript provides a number of built-in functions that programmers expect to find in a high level programming language. These include: eval(); parseInt(); parseFloat(). You have already seen some examples of built in functions in use such as:

	alert("Your alert message");

We’ll be coming back to these but for now acquaint your self with other built in functions by following the next exercise:

Exercise

Pay a visit to the following 2 sites and review what they have to say about built-in functions:

User Defined Functions

Functions are created using the function keyword. This is followed by the name chosen for the function, a list of parameter names in brackets and then the actual Javascript code statements that make up the function enclosed within curly braces. E.g.

	function my_function(param1, param2, param3) {
		javascript code statements
		}

The name selected usually indicates what the function does. E.g. ‘checkform’, ‘totaliser’. The brackets after the name of the function contain the optional comma separated list of parameters that the function uses. These parameters are the variables that the function uses to do its job. For example, the following is the skeleton of a function to convert from celsius to fahrenheit:

	function temp_c_to_f(c_temp) {
		javascript code statements
		}

This function has only one parameter sensibly named c_temp which will contain the value of the temperature in degrees celsius. This will be the argument that is passed to the function.

Note: People are often confused by the terms ‘parameter’ and ‘argument’. Parameters are what the function uses to carry out its work. Arguments are the values or expressions allocated to those parameters when the function is used. Think of your home stereo system. It performs the function of playback from CD. To do so it uses certain parameters. E.g.:

  1. The track to be played;
  2. The volume level;
  3. The tonal settings;
  4. etc.

The values that are allocated to these parameters can be thought of as the arguments.

Calling a Function

Once a function has been defined it can be used by calling it. This is done by using its name followed by braces that enclose the list of arguments to be passed to the funtion.

Exercise

Open the following simple example: Simple Function Example. This uses a very simple XHTML form to prompt for some test which will then be presented from an alert dialogue box when the submit button is clicked.

Functions in Use

Take another look at the form example that was presented at the end of the forms page: My Form Example. Take a close look at the source code and complete the following exercise:

EXERCISE

With reference to the source code of the form example research and answer the following:

  1. In the head section of the form page the javascript ‘formvalidator’ function is defined. From where is it called?
  2. The code that calls this function looks like this: return formvalidator(this). What do the two words:
    • return
    • this

    do?

  3. The ‘formvalidator’ function calls one other function from an ‘if’ statement. What is the function called?
  4. How might the ‘formvalidator’ function be extended to check other fields in the form?

Leave a Comment