O'Reilly logo

Definitive VoiceXML™ by David Cuddihy, Adam Hocek

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A.5. Functions and objects

A function is a segment of ECMAScript code that can be called from other parts of a ECMAScript program. Functions are declared using the function keyword, for example:

function square(x){
  return x*x;
}

This function, which returns the square of a number, can be called as follows:

var y = square(4);

If the caller passes a built-in type to a function, it is passed by value. If the caller passes an object, it is passed by reference. A function can be made to return an object as well. The members of the object can be dynamically declared before the object is returned, for example:

function Employee(firstName, lastName, age){
  this.firstName = firstName;
  this.lastName = lastName;
  this.age = age;
  return this;
}

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required