Hey. I know some of you will probably laugh at me, and it's okay. I've been learning lua for over a year, I tried for a so long time learning how to return, but I never understood. Each time I watched a video/went to dev forum etc they didn't explain it so well. They always say the same thing, but this thing is something I can't understand. I get lua very well and I'm pretty sure it's a common mistake. Everyone is explaining it well but I need even more than that sadly. Can someone please try to explain me returning like I'm 4? It would be much appreciated. Thanks!
Can someone explain 'returning' to me as well as he can?
3 answers
Let's say you have a function (Math function) and you want to calculate a number with it
local function Add(x, y)
local TotalValue = x + y
end
print(Add(1, 2)) -- prints nothing
Now let's add a return statement to it
local function Add(x, y)
local TotalValue = x + y
return TotalValue
end
print(Add(1, 2)) -- prints 3
When you have function like this.
function add(a, b)
return a + b
end
what's that doing is adding that, and passing that variable to the line that called the function... For example
function add(a, b)
return a + b
end
print(add(5, 5)) --This will get 10
because it called the function, and the add function did the maths and RETURNED that variable to who called it...
Think of it like this...
You are a student, and you asked the teacher what is 1+1, and the teacher will tell you the answer...
In lua, you are a student (a line), and asked (called) the teacher (function), and will tell (return) you the answer (variable).
So basically that's what returning does, it's said in the name... When you call a function that has return on it, it will return / give you the value right next to it.
Also the difference between these two functions...
function add(a, b)
return a + b
end
and
function add(a, b)
print(a + b)
end
is that in the second function, it prints it directly. So it's the return one is more useful...
I really don't know how to explain stuff, so I hope you understand!
Please upvote :D
3 comments
You explained it really well. I'm taking a screenshot, I will read it multiplie times and try to understand. Thank you so much!\
You explained it really well. I'm taking a screenshot, I will read it multiplie times and try to understand. Thank you so much!
No problem!
The function return sends information to the server like printing etc. The return function also breaks a loop while its running your code without being referenced for e.g.
Here's an example script:
function hello()
print ("hi")
print ("hi")
return (10)
end
hello()
--[[
OUTPUT:
hi
hi
nil
]]
this code will yield an error and return nil(nothing) because you didn't call the function to print 10
function hello()
print ("hi")
print ("hi")
return (10)
end
print(hello())
--[[ Output:
hi
hi
10
]]--
This code is correct because you told the script to print the function hello therefore it will return 10 If you still don't understand I suggest watching this
Make sure to accept my answer and upvote if it helped took like 20 mins
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
It's appreciated that you tried. But I still do not understand. Thank you anyways for trying!