User-defined functions

<< Click to Display Table of Contents >>

Manual > Script Guide > Script Ver2 (SC2 syntax) >

User-defined functions

User-defined functions

User-defined functions are functions that developers can create themselves.

 

User-defined functions can be written for each object. Functions written in an object can be called and executed from other objects by absolute or relative path reference, just like methods and properties.

 

A user-defined function is written by writing the function name after the function keyword, and defining arguments in ( ) separated by commas (,). Function and argument names can be freely named. The return keyword can be used to end the function and return a return value.

 

The following is an example of a user-defined function.

 

function Total(a, b)
{
 return a + b;
}

 

In this example, the function name is "Total". When two arguments, a and b, are passed, the function returns the calculation result of "a+b".

 

 

User-defined function syntax

The syntax for a user-defined function is:

 

function FunctionName(, , …)

{

return ;

}

 

1)You can specify as many parameters as you need.

2)If no parameters are required, they can be omitted, but you must include "()" after the function name (for example, "function FunctionName()").

3)The return keyword allows you to exit a function and return a value to the caller.

4)The return value of return can be omitted. If the return value is omitted, the function will end at the "return" position.

5)You can write any number of return keywords in a script. You can also omit return itself. In that case, the function will terminate when the script has run to the end.
 
Example) If there is no return

function SampleFunction()
{
//

 
Example) If there is one return
function Total(a, b)
{
    return a + b;
}

 
Example) When there are multiple return
function ErrMessage(a)
{
 switch (a)
 {
  case 1: return "Error1";
  case 2: return "Error2";
  case 3: return "Errro3";
  }
  return "Not Found Error code";
}

 

6)You can return values through parameters by preceding the parameter with an "&" (pass by reference).
 
Example: Returning a value to an argument

function SampleFunction()
{
var Param;
GetParam(Param); //Param returns 100. 
}

function GetParam(&vParam)
{
vParam = 100; //Assign a value to the variable passed as an argument. 
}

 

 

attention

Recursive function calls are not supported.

 

hint

For information on naming user-defined functions, see "Naming Rules".