function Statement

Declares a new function. This can be used in several contexts:

// in the global scope
function functionname([parmlist]) [: type] {
   [body]
}

// declares a method in a class
[attributes] [modifiers] function functionname([parmlist]) [: type] {
   [body]
}

// declares a method in an interface
[attributes] [modifiers] function functionname([parmlist]) [: type]

Arguments

  • attributes
    Optional. Attributes that control the visibility and behavior of the method.

  • modifiers
    Optional. Modifiers that control the visibility and behavior of the method.

  • functionname
    Required. The name of the function or method.

  • paramlist
    Optional. A comma delimited parameter list for the function or method. Each parameter may include a type specification. The last parameter may be a parameterarray, which is denoted by three periods (...) followed by a parameter array name followed by a type annotation of a typed array.

  • type
    Optional. Return type of the method.

  • body
    Optional. One or more statements that define how the function or method operates.

Remarks

Use the function statement to declare a function for later use. The code contained in the body is not executed until the function is called from elsewhere in the script. The return statement is used to return a value from the function. You do not have to use a return statement, the program will return when it gets to the end of the function.

Methods are similar to global functions, except that they are scoped to the class or interface where they are defined and may have various modifiers governing their visibility and behavior. A method in an interface cannot have a body, while a method in a class must have a body. There is an exception to this rule; if a method in a class is abstract or the class is abstract, the method cannot have a body.

You may use type annotation to declare what data type the function or method returns. If void is specified as the return type, no value may be returned by any of the return statements inside the function. If any return type other than void is specified, all return statements in the function must return a value that is coercible to the specified return type. The value undefined is returned if a return type is specified, but a return statement appears with no value or if the end of the function is reached without a return statement. Constructor functions cannot specify a return type, since the new operator automatically returns the object being created.

If no explicit return type is specified for the function, the return type is set to either Object or void. The void return type is selected only when there are no return statements or the return statements appear with no value in the function body.

A parameter array can be used as the last the parameter of a function. Any additional arguments passed to the function (if any) after the required parameters will be entered into the parameter array. The type annotation of the parameter is not optional; it must a typed array. To accept parameters of arbitrary types, use Object[] as the typed array. When calling a function that can accept a variable number of arguments, an explicit array of the expected type may be used in place of supplying a list of parameters.

When calling a function, make sure that you always include the parentheses and any required arguments. Calling a function without parentheses causes the text of the function to be returned instead of the results of the function.

Example 1

The following example illustrates the use of the function statement in the first syntax:

interface IForm {
   // This is using function in Syntax 3.
   function blank() : String;
}

class CForm implements IForm {
   // This is using function in Syntax 2.
   function blank() : String {
      return("This is blank.");
   }
}

// This is using function in Syntax 1.
function addSquares(x : double, y : double) : double {
   return(x*x + y*y);
}

// Now call the function.
var z : double = addSquares(3.,4.);
print(z);

// Call the method.
var derivedForm : CForm = new CForm;
print(derivedForm.blank());

// Call the inherited method.
var baseForm : IForm = derivedForm;
print(baseForm.blank());

The output from this program is:

25
This is blank.
This is blank.

Example 2

In this example, a function printFacts takes as input a String and a used a parameter array to accept a variable number of Objects.

function printFacts(name : String, ... info : Object[]) {
   print("Name: " + name);
   print("Number of extra information: " + info.length);
   for (var factNum in info) {
      print(factNum + ": " + info[factNum]);
   }
}

// Pass several arguments to the function.
printFacts("HAL 9000", "Urbana, Illinois", new Date(1997,0,12));
// Here the array is intrepeted as containing arguments for the function.
printFacts("monolith", [1, 4, 9]);
// Here the array is just one of the arguments.
printFacts("monolith", [1, 4, 9], "dimensions");
printFacts("monolith", "dimensions are", [1, 4, 9]);

This program displays the following output when run:

Name: HAL 9000
Number of extra information: 2
0: Urbana, Illinois
1: Sun Jan 12 00:00:00 PST 1997
Name: monolith
Number of extra information: 3
0: 1
1: 4
2: 9
Name: monolith
Number of extra information: 2
0: 1,4,9
1: dimensions
Name: monolith
Number of extra information: 2
0: dimensions are
1: 1,4,9

Requirements

Version 1 (for syntax 1)Version .NET (for syntaxes 2 and 3)

See Also

Concepts

Scope of Variables and Constants

Type Annotation

Typed Arrays

Reference

new Operator

class Statement

interface Statement

return Statement

Other Resources

Modifiers