Difference between revisions of "Return/ja"

From Second Life Wiki
Jump to navigation Jump to search
(New page: {{Multi-lang}} {{LSL Header/ja| }}{{#vardefine:name|return }}{{#vardefine:p_value_desc|関数による値もしくは変数が返され、関数によって返される型とあわせな...)
 
m
 
(One intermediate revision by one other user not shown)
Line 25: Line 25:
</div>
</div>
}}{{#vardefine:examples|
}}{{#vardefine:examples|
<pre>
<source lang="lsl2">
integer Goodbye()
integer Goodbye()
{
{
Line 37: Line 37:
     return;
     return;
}
}
</pre>
</source>
<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|*コンパイラにバグがあり、値とイベントを変えそうとする場合、ランタイムはスクリプトのクラッシュに遭遇するでしょう。
}}{{#vardefine:caveats|*コンパイラにバグがあり、値とイベントを変えそうとする場合、ランタイムはスクリプトのクラッシュに遭遇するでしょう。
Line 52: Line 87:
}}{{#vardefine:also_tests|
}}{{#vardefine:also_tests|
}}{{#vardefine:location|
}}{{#vardefine:location|
}}{{LSL Generic/ja}}{{LSLC|Flow Control/ja}}
}}{{LSL Generic/ja}}{{LSLC/ja|Flow Control}}

Latest revision as of 09:14, 26 September 2023

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

return value;

return value;
• type value 関数による値もしくは変数が返され、関数によって返される型とあわせなければなりません。

値が一つだけ実行する前の範囲に返して使用されます。

関数

関数が終了すると、呼び出したポイントに戻ってスクリプトが継続します。

イベント

スクリプトがクラッシュします。イベントは値を返すことができません。 このキーワードの代わりに次の方法を使いましょう。

return;

関数やイベントが最後まで完了するよりも早くreturnを実行する場合に使われます。 イベントや関数を最後まで行う、とコンパイラによって見込ませたい場合は使うべきではありません。

関数

関数が終了すると、呼び出したポイントに戻ってスクリプトが継続します。

イベント

イベントが終了すると、イベントのキューから除去されます。もしほかのイベントキューが存在するなら、そのイベントが実行されます。

注意点

  • コンパイラにバグがあり、値とイベントを変えそうとする場合、ランタイムはスクリプトのクラッシュに遭遇するでしょう。

サンプル

integer Goodbye()
{
    llOwnerSay("Goodbye");
    return 0;
}

Hello()
{
    llOwnerSay("Hello");
    return;
}
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) ] ) );
    }
}

参考情報

キーワード

•  jump
•  state