Frosteus
|
  |
| Joined: 21 Dec 2013 |
| Total Posts: 13470 |
|
|
| 14 Aug 2017 09:39 AM |
| wiki and tutorials confuse me more and more |
|
|
| Report Abuse |
|
|
|
| 14 Aug 2017 09:42 AM |
function ReturnOne() return 1 end
print(ReturnOne())
Šçrätčh ïïś üśëd bÿ łëvëł 17 äñöñ DÖŠ HTMŁ hæx0rs whö üsë ŚQŁ++ ïïńśpëçt ëłæmęñtïïñg |
|
|
| Report Abuse |
|
|
Frosteus
|
  |
| Joined: 21 Dec 2013 |
| Total Posts: 13470 |
|
| |
|
iiNemo
|
  |
| Joined: 22 Jul 2013 |
| Total Posts: 2380 |
|
|
| 14 Aug 2017 09:48 AM |
function add(num1, num2) print("Finding sum.") return num1 + num2 --print("Sum found") end x = add(5, 2) print(x)
Output: 7 |
|
|
| Report Abuse |
|
|
Frosteus
|
  |
| Joined: 21 Dec 2013 |
| Total Posts: 13470 |
|
|
| 14 Aug 2017 09:51 AM |
| shoving code tht i alredy read on the wiki doesnt help x.x |
|
|
| Report Abuse |
|
|
iiNemo
|
  |
| Joined: 22 Jul 2013 |
| Total Posts: 2380 |
|
|
| 14 Aug 2017 09:54 AM |
function Add(Num1, Num2) return Num1 + Num2 end
Add(5, 1) -- Num1 will equal 5 num 2 will equal 1
return will transform Add(5, 1) to be 5 + 1 which is six so like
if Add(5, 1) == 6 then print('NemoIsAwesome') end
|
|
|
| Report Abuse |
|
|
sonihi
|
  |
| Joined: 27 Jun 2009 |
| Total Posts: 3655 |
|
|
| 14 Aug 2017 10:00 AM |
So you may have a function that calculates something very complicated and you don't wan't to paste the code that calculates it everytime like math.log(a*b^2).
So you make a function:
function ComplexCalculation(a,b) return math.log(a*b^2) end
And now you try to calculate this complicated thing for numbers 4 and 3
ReturnedValue=ComplexCalculation(4,3)
Now the function "returns" or gives you the value and writes it in the Returnedvalue variable. |
|
|
| Report Abuse |
|
|
1pie23
|
  |
| Joined: 11 Jul 2010 |
| Total Posts: 1865 |
|
|
| 14 Aug 2017 10:08 AM |
return also stops remaining code from running:
local val = false
if val == false then return end
print("Do you see this in output?")
Hint: you don't see it in the output. |
|
|
| Report Abuse |
|
|
Frosteus
|
  |
| Joined: 21 Dec 2013 |
| Total Posts: 13470 |
|
| |
|
sonihi
|
  |
| Joined: 27 Jun 2009 |
| Total Posts: 3655 |
|
|
| 14 Aug 2017 10:17 AM |
Just try to use return in a couple examples.
Try this:
function add(Number1,Number2) local sum=Number1+Number2 return sum end
a=add(3,5) print(a)
and another thing
function add(Number1,Number2) local sum=Number1+Number2 end
a=add(3,5) print(a) |
|
|
| Report Abuse |
|
|
|
| 14 Aug 2017 10:17 AM |
| it takes values and then gives them back to u. i find it to be just a glorified print function |
|
|
| Report Abuse |
|
|
Frosteus
|
  |
| Joined: 21 Dec 2013 |
| Total Posts: 13470 |
|
|
| 14 Aug 2017 10:19 AM |
wuts the difference between
function addNumbers(num1, num2) return num1 + num2 end a = addNumbers(3, 5) print(a)
with the return and without?
cuz the numbers get plugged in either way, no? |
|
|
| Report Abuse |
|
|
Frosteus
|
  |
| Joined: 21 Dec 2013 |
| Total Posts: 13470 |
|
|
| 14 Aug 2017 10:21 AM |
| waitwaitwait so return takes the sum and switches it with the numbers u took to make the sum!??!?! |
|
|
| Report Abuse |
|
|
RobuxLife
|
  |
| Joined: 19 Sep 2012 |
| Total Posts: 13336 |
|
|
| 14 Aug 2017 11:05 AM |
alright think of this
you made a function, right?
function OOF(num1, num2) return num1 + num2 end
now, you can make a variable BE that function so like
local x = OOF(5, 5)
basically, you're doing 5 + 5, and putting it into a variable. By doing this;
function OOF(num1, num2) print(num1 + num2) end
you are printing it not returning it. so OOF(5, 5) would print out 10. by doing the return one, you can put it into a variable, and then print that variable whenever you like, or do whatever you want with it
- RobuxLife | Discord - RobuxLife#4128 | |
|
|
| Report Abuse |
|
|
Saldor010
|
  |
| Joined: 20 Sep 2010 |
| Total Posts: 1035 |
|
|
| 14 Aug 2017 11:13 AM |
Return is one of the only ways a function can send output back to the caller.
For example, if you were a parent, and you told your kid to go clean his room, he may or may not do it. The only way you would know for sure that he did it, is to see for yourself by checking the room. So in Lua, we would write it something like this: -- function cleanRoom() local didTimmyWantToCleanHisRoom = false local roomIsClean = false if roomIsClean == true then return true else return false end end
roomIsClean = cleanRoom() print("Did timmy clean his room? "..roomIsClean) -- roomIsClean is the only variable you will ever see in this example, because it's the only variable ever returned. You can't see whether timmy wanted to clean his room or not, all you see is whether the room was actually cleaned.
I hope that made sense to you. |
|
|
| Report Abuse |
|
|