Bookshelf Home | Contents | Index | Search | PDF |
Siebel eScript Language Reference > Siebel eScript Commands >
Function Objects
A Function object holds the definition of a function defined in eScript. Note that in eScript, procedures are functions.
Syntax A
function funcName( [arg1 [, ..., argn]] )
{
body
}Syntax B
var funcName = new Function([arg1 [, ..., argn,]] body );
Parameter Description funcName The name of the function to be created arg1 [, ..., argn] An optional list of arguments that the function accepts body The lines of code that the function executesReturns
Whatever its code is set up to return. For more information, read return Statement.
Usage
Syntax A is the standard method for defining a function. Syntax B is an alternative way to create a function and is used to create Function objects explicitly.
Note the difference in case of the keyword Function between Syntax A and Syntax B. Function objects created with Syntax B (that is, the Function constructor) are evaluated each time they are used. This is less efficient than Syntax A—declaring a function and calling it within your code—because declared functions are compiled instead of interpreted.
Example
The following fragment of code illustrates creating a function AddTwoNumbers using a declaration:
function AddTwoNumbers (a, b)
{
return (a + b);
}The following fragment illustrates creating the same function using the Function constructor:
AddTwoNumbers = new Function ("a", "b", "return (a + b)");
The difference between the two is that when AddTwoNumbers is created using a declaration, AddTwoNumbers is the name of a function, whereas when AddTwoNumbers is created using the Function constructor, AddTwoNumbers is the name of a variable whose current value is a reference to the function created using the Function constructor.
length Property
The length property returns the number of arguments expected by the function.
Syntax
funcName.length
Returns
The number of arguments expected by funcName.
return Statement
The return statement passes a value back to the function that called it.
Syntax
return value
Returns
Not applicable
Usage
The return statement passes a value back to the function that called it. Any code in a function following the execution of a return statement is not executed.
Example
This function returns a value equal to the number passed to it multiplied by 2 and divided by 5.
function DoubleAndDivideBy5(a)
{
return (a*2)/5
}Here is an example of a script using the preceding function. This script calculates the mathematical expression
n = (10 * 2) / 5 + (20 * 2) / 5.
It then displays the value forn
, which is 12.function myFunction()
{
var a = DoubleAndDivideBy5(10);
var b = DoubleAndDivideBy5(20);
TheApplication().RaiseErrorText(a + b);
}
Bookshelf Home | Contents | Index | Search | PDF |
Siebel eScript Language Reference Published: 18 April 2003 |