JoeDev
|
  |
| Joined: 04 Oct 2011 |
| Total Posts: 6475 |
|
|
| 31 Jul 2017 09:12 PM |
I might not fully understand, but it all seems like it can be done without the return function. Is it an optimization thing? Or is there more. |
|
|
| Report Abuse |
|
|
vivivio
|
  |
| Joined: 23 Jan 2012 |
| Total Posts: 707 |
|
|
| 31 Jul 2017 09:15 PM |
| its only used when the function is going to return something |
|
|
| Report Abuse |
|
|
|
| 31 Jul 2017 09:15 PM |
| If you want to have a block of code able to be called anywhere, make a function of it. However, what if you want output from that function based on certain inputs? That is when you would use return, to 'return' that output. |
|
|
| Report Abuse |
|
|
|
| 31 Jul 2017 09:15 PM |
It's actually always used in a function, implicitly or not.
|
|
|
| Report Abuse |
|
|
Curite
|
  |
| Joined: 28 Jan 2014 |
| Total Posts: 60 |
|
|
| 31 Jul 2017 09:15 PM |
| Oh fuck me daddy!!!!!!!! |
|
|
| Report Abuse |
|
|
|
| 31 Jul 2017 09:18 PM |
| ^Applause to the Roblox filter. |
|
|
| Report Abuse |
|
|
JoeDev
|
  |
| Joined: 04 Oct 2011 |
| Total Posts: 6475 |
|
|
| 31 Jul 2017 09:21 PM |
Is it to ensure its running properly?
Sorry, I am having a rough time. |
|
|
| Report Abuse |
|
|
|
| 31 Jul 2017 09:22 PM |
Used to return a value from a function to where you called it, an example:
fuction Add(a,b) return a + b end
local ReturnedValue = Add(5,2)
print(ReturnedValue) --This would then end up printing "7" |
|
|
| Report Abuse |
|
|
JoeDev
|
  |
| Joined: 04 Oct 2011 |
| Total Posts: 6475 |
|
|
| 31 Jul 2017 09:24 PM |
| I think I understand, is this commonly used? I just want to make sure I completely understand before I continue my learning. |
|
|
| Report Abuse |
|
|
|
| 31 Jul 2017 09:26 PM |
a void method doesn't return anything and is very common
any other method does meaning you could pass an entire function in as a parameter
pseudocode that may or may not work:
functionA() print("this will always return 5") return 5 end
functionB(num) print("this is a void method that won't return anything but its function is to multiply an integer by three") print(num * 3) end
functionB(functionA()); -- should print 15 |
|
|
| Report Abuse |
|
|
|
| 31 Jul 2017 09:27 PM |
yes it is commonly used
it's also needed when you're working with modulescripts http://wiki.roblox.com/index.php?title=API:Class/ModuleScript |
|
|
| Report Abuse |
|
|
iiNemo
|
  |
| Joined: 22 Jul 2013 |
| Total Posts: 2380 |
|
|
| 31 Jul 2017 09:29 PM |
Okay so there are many cases, example. while wait() do if blah == true then return end end this is practically the same thing as break, but ya. function Nemo() return 5 end print(Nemo()) Output: 5 Module scripts to return whats being called or whatever, I'm sure there are many others
|
|
|
| Report Abuse |
|
|
|
| 31 Jul 2017 09:30 PM |
| a function that does something and returns a boolean true/false can be especially useful |
|
|
| Report Abuse |
|
|
|
| 31 Jul 2017 09:33 PM |
outside of roblox if you get into any common object oriented programming language like java returns are highly necessary
module scripts in roblox are a great example of OOP and returns are necessary and added by default
the entirety of the module script can be done before it returns and only when it does return will the script that called it continue to run
|
|
|
| Report Abuse |
|
|
|
| 31 Jul 2017 09:40 PM |
@unsub indeed returns are always used in functions whether you supply one or not.
From a low level perspective a function must always return to the instruction after the call to the function.
for example a procedure in assembly languages will always have a RET instruction in order to return the instruction pointer to the address of the instruction after the call to the procedure.
myproc PROC PUSH EBP MOV EBP, ESP ;do something crazy MOV ESP, EBP POP EBP RET ;return myproc ENDP
main PROC PUSH EBP MOV EBP, ESP CALL myproc MOV ESP, EBP POP EBP RET ;return main ENDP
From a high level perspective like Lua, you would supply a return to one or more paths of your function to commonly return a value. Lets say that you wanted to return the sum of x and y in the following function, you can do so by returning it:
local function ReturnSum(x, y) return x + y; end local result = ReturnSum(10, 15); print(result); --will output 25. |
|
|
| Report Abuse |
|
|