Difference between revisions of "Return"

From Second Life Wiki
Jump to navigation Jump to search
m (Interesting MediaWiki bug!)
(Supplied a more sensible example)
Line 35: Line 35:
     return;
     return;
}</lsl>
}</lsl>
<lsl>// Very great thanks to Darling Brody for helping me understand this.
<lsl>
 
integer Calc (string cmd, integer a, integer b)
string WhoAreYou(key id)
{
{
     return (llKey2Name(id)); // return the value established
     // The user function is declared as returning an integer,
    // so every return within the function must return an integer
    if (cmd == "+")    return a + b;
    // We don't need to code 'else' here, as the preceding statement resulted in 'return' when true
    if (cmd == "-")    return a - b;
    if (cmd == "*")   return a * b;
    if (cmd == "/")   return a / b;
    // We must return a value here to, we can't omit the final return
    return -1; 
}
}
default
default
{
{
     touch_start(integer detected)
     state_entry()
     {
     {
         string name = WhoAreYou(llDetectedKey(0));  // store the name of the one who touched the object
         llSay(0, llList2CSV ( [ Calc("+",1,2), Calc ("-",5,3), Calc ("*",3,4), Calc("/",18,6) ] ) );
        if(name != "Philip Linden")
        {
            return;  // leave the event because the toucher is not "Philip Linden"
        }
        llSay(0, "OMG PHILIP LINDEN TOUCHED MA BOX!!!"); // just Philip Linden may read this in case he's ever bored and tests this script
     }
     }
}</lsl>
}
</lsl>
}}{{#vardefine:notes|
}}{{#vardefine:notes|
}}{{#vardefine:caveats|*There is a bug in the compiler and it will let you return a value with events, which when encountered at runtime will cause the script to crash.
}}{{#vardefine:caveats|*There is a bug in the compiler and it will let you return a value with events, which when encountered at runtime will cause the script to crash.

Revision as of 03:42, 29 December 2012

The correct title of this article is return. The initial letter is shown capitalized due to technical restrictions.

return value;

return value;
• type value value or variable to be returned by the function, the type must be the same as that to be returned by the function.

Used to return execution to the previous scope along with a value.

Functions

Exits the function and continues script execution in the previous scope.

Events

Causes the script to crash. Events cannot return a value. Use the next form of this keyword instead.

return;

Used to prematurely return execution to the previous scope before reaching the end of the function/event. You do not need to use this at the end of an event or function, as it is assumed by the compiler.

Functions

Exits the function and continues script execution in the previous scope.

Events

Exits the event and removes it from the event queue. If there is another event in the queue, that event is triggered.

Caveats

  • There is a bug in the compiler and it will let you return a value with events, which when encountered at runtime will cause the script to crash.
    • Do not try to return a value in the event scope if you do not wish your script to crash.

Examples

<lsl>integer Goodbye() {

   llOwnerSay("Goodbye");
   return 0;

}

Hello() {

   llOwnerSay("Hello");
   return;

}</lsl> <lsl> integer Calc (string cmd, integer a, integer b) {

   // The user function is declared as returning an integer,
   // so every return within the function must return an integer
   if (cmd == "+")    return a + b;
   // We don't need to code 'else' here, as the preceding statement resulted in 'return' when true
   if (cmd == "-")    return a - b;
   if (cmd == "*")    return a * b;
   if (cmd == "/")    return a / b;
   // We must return a value here to, we can't omit the final return
   return -1;   

} default {

   state_entry()
   {
       llSay(0, llList2CSV ( [ Calc("+",1,2), Calc ("-",5,3), Calc ("*",3,4), Calc("/",18,6) ] ) );
   }

} </lsl>

See Also

Keywords

•  jump
•  state