User Functions

The Snippet language supports custom user functions that can be declared in one Snippet and used elsewhere within the script. To declare a user function, simply use the FUNCTION key word followed by the function name, open parenthesis, any custom parameters, and a close parenthesis. Then enclose the function’s code within curly braces. The definition looks like this:

FUNCTION <name>([<param1>[, <param2>[ ... ] ] ]) { ... [RETURN [<value>] ] }

The name must not conflict with any built-in functions, but virtually any other name will work as long as invalid characters are not used. Valid characters for the name are the same as valid characters for any variable name. These include a-z, A-Z, 0-9 (after the first character), and underscore (_).

2 Types of Functions

The following types of functions are generally recognized: 1) those that return values, and 2) those that do not. A function that returns a value may be used in a place where a variable or expression is used. Functions that do not return a value must be used like a command (or statement). The following is an example of a function that returns a value:

FUNCTION MyAppend(a,b)
{
   RETURN "{a}{b}"
}	
	

Example use:

FOR i=1 TO 9
{
   ph = "{MyAppend(ph,i)}"
}
	Result: ph="123456789"
	

Another example use, using a function as if it were a method:

FOR i=1 TO 9
{
   ph = "{ph.MyAppend(i)}"
}
	

Either example above produces the same result. Using the function in the form: <var>.<function>(…) is just syntactical sugar to make use of the function more object-oriented-like. It also enables Intelli-Prompt help on the function. Adding a comment on the line with the function declaration includes the comment as help text, illustrated in the following example.

Example of a function that does not return a value:

FUNCTION generateskill(a)
{
   skill = random(a)
}
	

Example use:

generateskill(100)
	
	Sample result: skill="58"
		

Variable Visibility

Variables defined within a function may or may not be visible outside the function depending on the mode of use. When a function is used like a command (as shown in the second example above), all variables defined within the function will be visible outside the function. The function just acts as an extension to the Snippet code. However, when a function is used in an expression (like the first example above), all variables defined within the function will be lost when the function returns. Only the value in the RETURN clause will be returned to the context of the calling expression.

A function with comments:

Image displaying example of the Intelli-Prompt support for a function.

The example above shows the Intelli-Prompt support for a function when accessed like a method. It appears in the drop-down list of methods with an alternate icon and the help text shows the comment as the description.

Stand-alone Functions

In addition to variable methods, there are a handful of stand-alone functions. Stand-alone functions do not require an existing variable and may be placed anywhere a string or numeric expression is allowed. For example, the random function will produce a random value as in “Your wait time is approximately {random(10)} minutes”.

Format Specifiers

Numeric Format Strings