AutoPatrol, Bureaucrats, Administrators
683
edits
Line 130: | Line 130: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
= Functions = | = Functions = | ||
A function is a named code block that is generally intended to process specified input values into an output value, although this is not always the case. For example, the [[NXSL:trace|trace]] function takes variables and static text and prints the values into server log. Like many languages, NXSL provides for user-defined functions. These may be located anywhere in the main program or loaded in from other scripts via the use keywords. | |||
To define a function, you can use the following form: | |||
'''sub''' ''NAME'' '''(''' ''ARGUMENTS'' ''')''' '''BLOCK''' | |||
where ''NAME'' is any valid identifier, ''ARGUMENTS'' is optional list of argument names, and ''BLOCK'' is code block. | |||
To call a function you would use the following form: | |||
''NAME'' '''(''' ''LIST'' ''')''' | |||
where ''NAME'' is identifier used in function definition, and ''LIST'' is an optional list of expressions passed as function arguments. | |||
To give a quick example of a simple subroutine: | |||
<syntaxhighlight lang="c"> | |||
sub message() | |||
{ | |||
println "Hello!"; | |||
} | |||
</syntaxhighlight> | |||
== Function Arguments == | |||
The first argument you pass to the function is available within the function as $1, the second argument is $2, and so on. For example, this simple function adds two numbers and prints the result: | |||
<syntaxhighlight lang="c"> | |||
sub add() | |||
{ | |||
result = $1 + $2; | |||
println "The result was: " . result; | |||
} | |||
</syntaxhighlight> | |||
To call the subroutine and get a result: | |||
<syntaxhighlight lang="c"> | |||
add(1, 2); | |||
</syntaxhighlight> | |||
If you want named arguments, list of aliases for $1, $2, etc. can be provided in function declaration inside the brackets: | |||
<syntaxhighlight lang="c"> | |||
sub add(numberA, numberB) | |||
{ | |||
result = numberA + numberB; | |||
println "The result was: " . result; | |||
} | |||
</syntaxhighlight> | |||
If parameter was not provided at function call, value of appropriate variable will be '''null'''. | |||
== Return Values from a Function == | |||
You can return a value from a function using the '''return''' keyword: | |||
<syntaxhighlight lang="c"> | |||
sub pct(value, total) | |||
{ | |||
return value / total * 100.0; | |||
} | |||
</syntaxhighlight> | |||
When called, return immediately terminates the current function and returns the value to the caller. If you don't specify a value in '''return''' statement or function ends implicitly by reaching end of function's block, then the return value is '''null'''. | |||
Line 138: | Line 203: | ||
A ''key'' must be a non-negative integer. When an array is created, its size is not specified and its map can have empty spots in it. For example, an array can have a element with a ''0'' key and an element with ''4'' key and no keys in-between. Attempting to access an array key which has not been defined is the same as accessing any other undefined variable: the result will be <tt>NULL</tt>. | A ''key'' must be a non-negative integer. When an array is created, its size is not specified and its map can have empty spots in it. For example, an array can have a element with a ''0'' key and an element with ''4'' key and no keys in-between. Attempting to access an array key which has not been defined is the same as accessing any other undefined variable: the result will be <tt>NULL</tt>. | ||
= Operators = | = Operators = |