Zeloi
|
  |
| Joined: 27 Sep 2010 |
| Total Posts: 1143 |
|
|
| 10 May 2014 09:07 AM |
Ok so I've been scripting for maybe around 1-2 years and have NEVER found out how to use return and when to use it.
Can some of you explain return in-depth?
Thanks! |
|
|
| Report Abuse |
|
|
Trioxide
|
  |
| Joined: 29 Mar 2011 |
| Total Posts: 32902 |
|
|
| 10 May 2014 09:09 AM |
myFunction = function(x, y) return x + y end
myNumber = myFunction(10,25) print(myNumber) --> 35 print(myFunction(10,20)) --> 30 |
|
|
| Report Abuse |
|
|
Trioxide
|
  |
| Joined: 29 Mar 2011 |
| Total Posts: 32902 |
|
|
| 10 May 2014 09:09 AM |
In a nutshell: http://wiki.roblox.com/index.php/Function#Using_Return |
|
|
| Report Abuse |
|
|
|
| 10 May 2014 09:09 AM |
function is_it_true(text) if text == "yes" then return true elseif text == "no" then return false end end
print(is_it_true("yes")) >true
function getStringFromNumber(number) if number == 1 then return "lolz" else return "lol ASDSD" end end
print(getStringFromNumber(1)) >lolz |
|
|
| Report Abuse |
|
|
Zeloi
|
  |
| Joined: 27 Sep 2010 |
| Total Posts: 1143 |
|
|
| 10 May 2014 09:11 AM |
Explain a bit?
I don't need a script I just need an explanation |
|
|
| Report Abuse |
|
|
|
| 10 May 2014 09:12 AM |
Did you read trioxide's link? It explains it.
From the link:
When return is called, a couple things happen:
The function stops executing; also note that you cannot have any statements immediately after return or you will receive an error. The function becomes somewhat like a variable, holding the value(s) returned. Like this you can "set" a variable to what the function returns. |
|
|
| Report Abuse |
|
|
Zeloi
|
  |
| Joined: 27 Sep 2010 |
| Total Posts: 1143 |
|
|
| 10 May 2014 09:13 AM |
| so return prints a number value? |
|
|
| Report Abuse |
|
|
|
| 10 May 2014 09:15 AM |
Nononono, return "gives" whatever you want it to give, from the function to the scope you call it in.
Lets say we have a function like this:
function getPlayerFromName(name) if game.Players:findFirstChild(name) then return game.Players[name] --returns the player instance end end
player = getPlayerFromName("StealthKing95") player:Kick() |
|
|
| Report Abuse |
|
|
lewisw60
|
  |
| Joined: 27 Nov 2008 |
| Total Posts: 193 |
|
|
| 10 May 2014 09:16 AM |
If you return a function, then you can call a function using a variable.
function foo() bar = 'anything' return bar end
x = foo() print(x)
That would print 'anything' |
|
|
| Report Abuse |
|
|
Zeloi
|
  |
| Joined: 27 Sep 2010 |
| Total Posts: 1143 |
|
|
| 10 May 2014 09:19 AM |
| xD IM SOOOOO Confused im sorry! qq |
|
|
| Report Abuse |
|
|
lewisw60
|
  |
| Joined: 27 Nov 2008 |
| Total Posts: 193 |
|
|
| 10 May 2014 09:22 AM |
Basically you can give the actual function a value. Almost like a variable. so function yes() return 1 end
would make the function yes worth 1 you could go on to say x = yes so now x = 1 because yes = 1
|
|
|
| Report Abuse |
|
|
|
| 10 May 2014 09:23 AM |
^ small error here. Youre actually setting x to the function, not what its returning.
change x = yes
to
x = yes() |
|
|
| Report Abuse |
|
|
Zeloi
|
  |
| Joined: 27 Sep 2010 |
| Total Posts: 1143 |
|
|
| 10 May 2014 09:24 AM |
Ok I get it but why would you use this?
would you use it to upper a value like if you where making a tycoon? |
|
|
| Report Abuse |
|
|
lewisw60
|
  |
| Joined: 27 Nov 2008 |
| Total Posts: 193 |
|
| |
|
Zeloi
|
  |
| Joined: 27 Sep 2010 |
| Total Posts: 1143 |
|
|
| 10 May 2014 09:31 AM |
| What would I use this for? |
|
|
| Report Abuse |
|
|