Difference between revisions of "User-defined functions"
Jump to navigation
Jump to search
(quick description of user-defined functions in LSL) |
m |
||
Line 1: | Line 1: | ||
{{LSL Header}} | |||
LSL allows user-defined functions. The syntax is very simple: | LSL allows user-defined functions. The syntax is very simple: | ||
Revision as of 18:39, 5 October 2007
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
LSL allows user-defined functions. The syntax is very simple:
return_type variable_name(par1type par1name, par2type par2name,...) { function_statements; . . . return value_of_return_type; }
There are no "function" or "def" keywords or function type/inheritance specifiers needed. I have not yet discovered what limits (if any) exist on the length or composition of the parameter list. Multiple types can be used, as shown in the example below. A list can be returned, so a single function can return a list with several values in it. User-defined functions will help keep scripts compact and readable, and allow rudimentary code reuse. The LSL_Style_Guide suggests that user-defined functions should be placed before the default state definition and after user-defined variables.
Here are a couple of simple examples:
float xsquared(float x) // return the square of the argument { return x*x; } vector scalarmult(vector vIn, float sMult) // return the product of a scalar and a vector { vIn.x=vIn.x*sMult; vIn.y=vIn.y*sMult; vIn.z=vIn.z*sMult; return vIn; } default { state_entry() { llWhisper(0, "Some user functions"); } touch_start(integer total_number) { float testx=3; llWhisper(0,"testx squared :"+(string)xsquared(testx)); vector testVector = <1,2,3>; testVector = scalarmult(testVector,testx); llWhisper(0,"test vector is:"+(string)testVector); } }