Difference between revisions of "Return"
Jump to navigation
Jump to search
(First 3 example are misleading and promote poor coding practices. Replaced with one example that illustrates the proper usage of the return keyword.) |
|||
(17 intermediate revisions by 8 users not shown) | |||
Line 1: | Line 1: | ||
{{LSL Header| | {{LSL Header|ml=*}} | ||
}}{{#vardefine:name|return | {{#vardefine:name|return | ||
}}{{#vardefine:p_value_desc|value or variable to be returned by the function, the type must be the same as that to be returned by the function. | }}{{#vardefine:p_value_desc|value or variable to be returned by the function, the type must be the same as that to be returned by the function. | ||
}}{{#vardefine:header_title|return {{LSL Param|value}}; | }}{{#vardefine:header_title|return {{LSL Param|value}}; | ||
Line 24: | Line 24: | ||
</div> | </div> | ||
}}{{#vardefine:examples| | }}{{#vardefine:examples| | ||
< | <source lang="lsl2"> | ||
string get_name(key uuid) | |||
{ | { | ||
return llKey2Name(uuid); // Returns the value of llKey2Name(). | |||
} | } | ||
// Usage within the script: | |||
llOwnerSay("Name: " + get_name( llDetectedKey(0) ) ); // Tell owner what the name is. | |||
</source> | |||
<source lang="lsl2"> | |||
integer Calc (string cmd, integer a, integer b) | |||
{ | |||
// The user function has been 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 too, 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) ] ) ); | |||
} | |||
} | } | ||
</ | </source> | ||
}}{{#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. | ||
**Do not try to return a value in the event scope if you do not wish your script to crash. | **Do not try to return a value in the event scope if you do not wish your script to crash. | ||
}}{{#vardefine:helpers| | }}{{#vardefine:helpers| | ||
}}{{#vardefine:also_header|<h3>Keywords</h3> | |||
{{{!}} | |||
{{LSL DefineRow||[[jump]]|}} | |||
{{LSL DefineRow||[[state]]|}} | |||
{{!}}} | |||
}}{{#vardefine:also_footer| | }}{{#vardefine:also_footer| | ||
}}{{#vardefine:also_functions| | }}{{#vardefine:also_functions| | ||
Line 46: | Line 73: | ||
}}{{#vardefine:also_articles| | }}{{#vardefine:also_articles| | ||
}}{{#vardefine:also_tests| | }}{{#vardefine:also_tests| | ||
}}{{#vardefine:location| | }}{{#vardefine:location| | ||
}}{{LSL Generic}}{{LSLC|Flow Control}} | }}{{LSL Generic}}{{LSLC|Flow Control}} |
Latest revision as of 00:41, 5 March 2016
LSL Portal | Functions | Events | Types | Operators | Constants | Flow Control | Script Library | Categorized Library | Tutorials |
- 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
string get_name(key uuid)
{
return llKey2Name(uuid); // Returns the value of llKey2Name().
}
// Usage within the script:
llOwnerSay("Name: " + get_name( llDetectedKey(0) ) ); // Tell owner what the name is.
integer Calc (string cmd, integer a, integer b)
{
// The user function has been 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 too, 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) ] ) );
}
}