User:Anaminus/return nutshell: Difference between revisions
From Legacy Roblox Wiki
Jump to navigationJump to search
>Anaminus 'return' in a nutshell |
>Anaminus mNo edit summary |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
{{CatUp|User:Anaminus}} | |||
="return" in a nutshell....= | ="return" in a nutshell....= | ||
Line 106: | Line 108: | ||
<pre>DontEscape(0)</pre> | <pre>DontEscape(0)</pre> | ||
'it was 0' will print in the output. | 'it was 0' will print in the output. | ||
'the end' will print in the output. | 'the end' will print in the output. | ||
Line 166: | Line 169: | ||
'5' will print in the output. | '5' will print in the output. | ||
'6' will print in the output. | '6' will print in the output. | ||
'7' will print in the output. | '7' will print in the output. | ||
Line 182: | Line 187: | ||
'15' will print in the output. | '15' will print in the output. | ||
'16' will print in the output. | '16' will print in the output. | ||
'17' will print in the output. | '17' will print in the output. | ||
---- | ---- |
Latest revision as of 08:08, 12 April 2008
"return" in a nutshell....
The return statement is used to return values from a function.
Example:
function number() return 7 end print(number())
'7' will print in the output.
More Examples:
Variables can be used to return values,
function string() local s = "Message" return s end print(string())
'Message' will print in the output.
When assigning a function to a variable, the variable becomes what the function returns.
function number() return 11 end n = number() print(n)
'11' will print in the output.
'return' acts as output.
function add(value) v = value + 10 return v end n = add(5) print(n)
'15' will print in the output.
Another output example:
function add(value) return value + 10 end n = add(6) print(n)
'16' will print in the output.
'return'ing nothing returns nil
function nothing() return end n = nothing() print(n)
'nil' will print in the output.
'return'ing nothing is often used to escape a function
When not escaping:
function DontEscape(value) if value == 0 then print("it was 0") end print("the end") end
DontEscape(1)
'the end' will print in the output.
DontEscape(0)
'it was 0' will print in the output.
'the end' will print in the output.
When escaping:
function Escape(value) if value == 0 then print("it was 0") return end print("the end") end
Escape(1)
'the end' will print in the output.
Escape(0)
'it was 0' will print in the output.
'return' can be used to return multiple values.
function NotMultiple() return 10 return 20 end print(NotMultiple())
'10' will print in the output.
function Multiple() return 10,20 end print(Multiple())
'10 20' will print in the output.
Multiple variables can be set with multiple returns.
Normal:
a,b,c = 5,6,7 print(a) print(b) print(c)
'5' will print in the output.
'6' will print in the output.
'7' will print in the output.
Multiple:
function multiple() return 15,16,17 end a,b,c = multiple() print(a) print(b) print(c)
'15' will print in the output.
'16' will print in the output.
'17' will print in the output.