The function statement identifies a block of statements that are not executed until the function is called. A function can be called at any time and can be called multiple times. Functions are usually defined at the top of the script but they can be placed anywhere in the script as the server-side parser resolves all function references before processing starts.

Functions can be defined to receive parameter values from the calling statement. The function can receive more than one parameter and the calling statement does not have to supply all parameters specified in the function.

A function can return a value to the calling statement. This returned value is the variable defined in the ‘return’ statement.

Example 1

function aFunction() {
  write(“world");
}
write('Hello ');
aFunction();

Example 2

function bFunction(name1, name2, name3) {
  write(name1+", “+name2+", “+name3);
  return ' OK';
}
write(“Hello");
var result = bFunction(“Bill?, “Harry");
write(result);

In example 1 the code will write out “Hello world?. In example 2 the code will write out “Hello Bill, Harry, OK". Note that the calling code did not define name3 so the function used null, and that the function returned a string.

All functions automatically create an object called ‘list’ (this is a reserved word) that is an array of all the parameters specified by the calling statement. This list object, which is local to the function, allows the function to accept any number of parameters without having to pre-define them. The parameters can be used by standard array references, e.g. in example 2 above the parameter value “Bill" can be referenced by ‘list[0]’ and the value "Harry" can be referenced by ‘list[1]’.

It also worth noting that the function's parameter does not need to have the same name as the variable which holds it outside the function (e.g. variable is myName; parameter is name).

Passing Parameters by Value

If the data for a parameter is a primitive object (null, boolean, number or string) then the function is given a copy of the data. This copy is completely separate from the original data and can be changed independently of the data.

Example 3

function cFunction(name) {
  name = “John";
}
var myName = “Jim";
cFunction(myName);
write(“my name is “+myName);

In this example the code writes “my name is Jim? as the variable myName was changed inside the function but not outside it.

Passing Parameters by Reference

If the data for a parameter is a non-primitive object (i.e. any object created with the new operator or any object that has been given properties) then the function is given a reference to the object not a copy of the object. If the original data object is changed then the reference will also see the change, and if the reference is changed then the original data is changed.

Example 4

function dFunction(name) {
 name.age = 21;
}
var myName = “Jim";
myName.age = 18;
dFunction(myName);
write(“my name is “+myName+" and my age is “+myName.age);

In this example the variable 'myName' is a non-primitive object (because it holds a property) and so only a reference was passed to the function. Changing the age property in the function means that the code will write "my name is Jim and my age is 21".

Comment on this topic

Topic ID: 150056