|
| 01 Jun 2015 09:08 PM |
I know lua (not Roblox lua though) alright. I can make many differnt programs but I have yet to understand return. i.e.
-------------------------------------- --what is the difference between:
function add(num1, num2) print(num1 + num2) end
--and
function add(num1, num2) return num1 + num2 end ------------------------------------ Mainly when returning strings and numbers in functions, I do not understand what the advantage of it over print is. Help is much appreciated!! Thanks in advance.
|
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 01 Jun 2015 09:08 PM |
the first one will print the sum the second one will return the sum to the caller
|
|
|
| Report Abuse |
|
|
|
| 01 Jun 2015 09:09 PM |
return returns a value
function echo(x) return x end
b = echo("POTATO")
print(b)
that would print potato |
|
|
| Report Abuse |
|
|
|
| 01 Jun 2015 09:09 PM |
print is just a function that displays something in the output. Return returns back the value to the caller:
function add(num1, num2) return num1 + num2 end
local var = add(5, 5) --this variable is now 10
Whereas this:
function add(num1, num2) print(num1 + num2) end
local var = add(5,5) --this variable is nil |
|
|
| Report Abuse |
|
|
CrowClaws
|
  |
| Joined: 04 Jul 2010 |
| Total Posts: 4466 |
|
|
| 01 Jun 2015 09:10 PM |
@bigboy return works the same in almost all languages
It has advantages bc u dont' always pring stuffs. Say u wanted to make a function that'd return a value u want to use e.i
function add(num1, num2) return num1 + num2 end
print(add(1,2)*3)
or something |
|
|
| Report Abuse |
|
|
|
| 01 Jun 2015 09:10 PM |
What if you don't want to print it?
function add(num1,num2) return num1 + num2 end
Workspace.Baseplate.Position = add(56, 387) Instance.new("Explosion", Workspace).BlastRadius = add(18, 58) |
|
|
| Report Abuse |
|
|
|
| 01 Jun 2015 09:11 PM |
@kingkiller1000
Return doesn't print, it returns
dictionary.com |
|
|
| Report Abuse |
|
|
| |
|
|
| 01 Jun 2015 09:12 PM |
| When the hell did I say that? |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
| |
|
|
| 01 Jun 2015 09:16 PM |
| Yeah, I just noticed that. Editing mistake. |
|
|
| Report Abuse |
|
|