• Returning - What does it mean?
    36 replies, posted
Usually at the end of a function, there's a 'return' with something after it, I have a vague idea what it means but a clear explanation would be nice.
Example: [lua]function booz() return "foo" end print(booz()) [/lua] Output: foo Basicly booz outputs foo, which is sent to the print that prints it.
Returning basically means to send values back to where the function was called from. [lua]function foo(a, b) -- foo take two arguments return a+b -- and returns the addition of them end local r = foo(1, 4) -- r will now hold 1+4 print(r) -- Output: 5 as expected.[/lua] [editline]04:32PM[/editline] You can return any Lua value, such as tables, numbers, strings. You can also return multiple values, like this: [lua]function bar(a, b, c, d) return a+b, c+d end local r, s = bar(1, 4, 5, 5) print(r, s) -- Output: 5 10[/lua]
Ahhh, I get it now, thank you :)
Note, 'return' on it's own means "Stop the function here, I want to get off.", and is equivalent to 'return nil'
[QUOTE=Lexic;22063842]Note, 'return' on it's own means "Stop the function here, I want to get off.", and is equivalent to 'return nil'[/QUOTE] Don't quite get that..
Example: [lua]function foo() return print("bar") end foo() [/lua] Will not do anything becuse the return stops the function and jumps back to the line where it was called
Thanks. I dindt knew that.
Ohhhhhhhhhh, I see, so what's the difference between: [LUA] function foo() print("bar") return end foo() [/LUA] and [LUA] function foo() print("bar") end foo() [/LUA]
Ignoring the extra 'end' on the second one, nothing, other than you are explicitly saying 'return'. If you don't do that, Lua puts a 'return' at the end for you.
It doesent use the keyword return..??
theres no diff between them, as the return is at the end. it stops at the return
Ok. So the return doesent do anything in this example?
Return on it's own can also be used to slim down if pyramids. For example: [lua]function plugh() if (ply.foo) then print("You cannot foo!"); else bar() if (ply.baz) then print("You cannot baz!") else qux() if (ply.quux) then print("You cannot quux!") else garply() end end end end[/lua] Can be compressed down to [lua]function plugh() if (ply.foo) then print("You cannot foo!"); return end bar() if (ply.baz) then print("You cannot baz!") return; end qux() if (ply.quux) then print("You cannot quux!") return; end garply() end[/lua] Which I personally think looks much neater.
Is return "" the same as return?
Not really [lua]function A() return "" end print(type(A())) -- Output: String function B() return end print(type(B())) -- Output: nil [/lua]
[QUOTE=Drakehawke;22143693]Is return "" the same as return?[/QUOTE] returning "" returns an empty string, which is not nil.
[QUOTE=Tobba;22067161]Example: [lua]function foo() return print("bar") end foo() [/lua] Will not do anything becuse the return stops the function and jumps back to the line where it was called[/QUOTE] A bit late but note that this example is invalid. You can't have anything after a return statement in any given "block" of code. [editline]11:42AM[/editline] [url]http://www.lua.org/pil/4.4.html[/url]
[QUOTE=Crazy Quebec;22145375]A bit late but note that this example is invalid. You can't have anything after a return statement in any given "block" of code. [editline]11:42AM[/editline] [url]http://www.lua.org/pil/4.4.html[/url][/QUOTE] [QUOTE=Tobba;22067161]Example: [lua]function foo() return print("bar") end foo() [/lua] Will not do anything becuse the return stops the function and jumps back to the line where it was called[/QUOTE] Tobba's function is actually valid and will actually print 'bar' (which defeats the purpose of his example). however, this isn't: [lua]function foo() return; print("bar") end[/lua] In Tobba's example the function returns the return value from print("bar"), so the print actually gets called. In my example the semi-colon prevents this. [editline]04:48PM[/editline] This happens because Lua doesn't use whitespace to end statements.
[QUOTE=MakeR;22145501]Tobba's function is actually valid and will actually print 'bar' (which defeats the purpose of his example). however, this isn't: [lua]function foo() return; print("bar") end[/lua] In Tobba's example the function returns the return value from print("bar"), so the print actually gets called. In my example the semi-colon prevents this. [editline]04:48PM[/editline] This happens because Lua doesn't use whitespace to end statements.[/QUOTE] Ah you are right.
I just suck at adding spaces instead of tabs
[QUOTE=iRzilla;22145841]You serious? I thought semi colons were obsolete...[/QUOTE] They were never obsolete for they never were valid to use in lua. But Garry's Mod lua has them and they can be used to end a line, which is of course completely pointless to do in lua save for that very case Maker posted.
[QUOTE=Crazy Quebec;22145921]They were never obsolete for they never were valid to use in lua. But Garry's Mod lua has them and they can be used to end a line, which is of course completely pointless to do in lua save for that very case Maker posted.[/QUOTE] They are valid in pure Lua. [editline]05:11PM[/editline] [QUOTE=Tobba;22145877]I just suck at adding spaces instead of tabs[/QUOTE] Doesn't matter, it would happen with any white space (including tab). [editline]05:14PM[/editline] There is literally almost no case where you would do what Tobba posted anyway, normally a return would either be followed by the function end, or an else/elseif statement.
[QUOTE=|FlapJack|;22144039]returning "" returns an empty string, which is not nil.[/QUOTE] In what case would you want to return an empty string instead of just nil?
In [b][url=wiki.garrysmod.com/?title=Gamemode.PlayerSay]Gamemode.PlayerSay [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] I guess.
[QUOTE=MakeR;22145936]They are valid in pure Lua.[/QUOTE] Aw this ain't my day. I somehow always thought it was Garry who added them with all of his C syntax additions..
[QUOTE=MakeR;22146332]In [b][url=wiki.garrysmod.com/?title=Gamemode.PlayerSay]Gamemode.PlayerSay [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] I guess.[/QUOTE] confused
[QUOTE=Drakehawke;22148773]-confused face-[/QUOTE] For example, [lua]function DonatorChat( ply, text, toall ) if !ply:IsSuperAdmin() then return end if (string.sub(text, 1, 8) == "!donator") then ply:ConCommand("donator "..string.Right(text, string.len(text)-8)) return "" end end hook.Add( "PlayerSay", "DonatorChat", DonatorChat )[/lua] Thats what I use in my donation system. It stops !donator from showing in the chat.
[QUOTE=Drakehawke;22148773]-confused face-[/QUOTE] Well hopefully all the poeple replying to this will have made it easier to understand, Lua is a hard nut to crack, so to speak, but it will just come to you if you spend some time pushing your boundaries.
[QUOTE=Jamie932;22149108]For example, [lua]function DonatorChat( ply, text, toall ) if !ply:IsSuperAdmin() then return end if (string.sub(text, 1, 8) == "!donator") then ply:ConCommand("donator "..string.Right(text, string.len(text)-8)) return "" end end hook.Add( "PlayerSay", "DonatorChat", DonatorChat )[/lua] Thats what I use in my donation system. It stops !donator from showing in the chat.[/QUOTE] I understand all that apart from [lua] ply:ConCommand("donator "..string.Right(text, string.len(text)-8)) [/lua]
Sorry, you need to Log In to post a reply to this thread.