Don't click here unless you want to be banned.

LSL Wiki : CrashCourse4

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are crawl338.us.archive.org
Chapter Listing
< 1 2 3 4 5 6 7 8 9 >

4) Functions
A function, in general, does some work, and returns a result of a particular type. In LSL, as in C, a function can return a meaningless or nonexistent result -- in C, this is called the "void" type; in LSL, one simply excludes a type from the function's prototype (explained in a moment). Pedants will say that a function that doesn't return a result is actually a "procedure", but we won't get too worked up about that.

Anyway, here's an example function in LSL:
integer sum(integer a, integer b)
{
   integer result = a + b;
   return result;
}
Here we see the basic layout of a function: its calling convention (that is, the name of the function, how many arguments (AKA parameters) it takes, in what order they appear, what their types are, and how they're identified), sometimes also called a "prototype", followed by a code block (which of course, can contain sub-code-blocks).

Now, there are a few abstract ideas to get down:

1) A function can, inside of its code, call another function. Program flow in the caller stops temporarily, and control is passed to the function being called (which is sometimes referred to as the callee -- hey, don't blame me, I don't make these things up); when it returns, flow continues after the call.

Example:
integer negate(integer x)
{
   return -x;
}

integer difference(integer a, integer b)    //This isn't intended for efficiency
{
   integer negB = negate(b);
   integer result = a + negB;
   return result;                //If you wanted efficiency, it would be "return a - b;"
}
So, here, when you call difference with two numbers, first, the second argument (b) has its value passed to negate, which takes control, and negates the value passed to it, then returns the negated value of b. Control returns to the difference function, and the result is stored in negB. Then, we add the two operands. Adding a negative is equivalent to subtracting, so the difference function really does compute the difference between the two original arguments (a and b). Fantastic!

2) A function can take almost any number of arguments, including none:

Examples:
doNothing()
{
   // takes no arguments, and does nothing, and returns nothing -- what a waste!
}
integer add4Numbers(integer w, integer x, integer y, integer z)
{
   return w + x + y + z;
}

3) Arguments passed into a function are copies of values, and can be modified with no effect on the caller (note: this is specific to LSL, not programming in general).

Example:
// assume we have the negate function from before already defined
integer foo()
{
   integer q = 1;
   integer negQ = negate(q);
   // what is the value of q now? that's right, still 1 -- can you explain why?
}
The variable q is still 1 because a copy of its value was passed to the negate function, so when the negate function modified its argument, it didn't modify q, but rather a copy of the value that q had when the function was called. This is a very important concept. If you do not understand it fully --I mean crystal clarity-- IM another scripter in-world and ask them to explain it better. If they can't help you, they'll generally be able to point you in the right direction. Most problems students have in learning are not caused by their lack of capability, but by poor teaching.

Okay, now you might be thinking "great, now I have to implement all kinds of basic functions before I can even get to real scripting," and if so, cheer up, because it's not the case. There are in fact many, many useful built-in functions, and to help you keep track of them, they all begin with double-lowercase-ell ("ll"). (It stands for Linden Lab.) You can call these functions to determine where your avatar is, to change the color of a prim, and much more. See the Functions page for a complete list of built-in functions.

Previous | Next

Homepage | Tutorial | CrashCourse | Glossary
There is no comment on this page. [Display comments/form]