I'm way more advanced than this, but returning was always my weak spot. What exactly does it do? When are you supposed to use it? When I try to watch a video on it, they just use printing, I get it with printing but not other stuff. If possible please write a script using return that can use printing but just don't make it too simple.
It's actually a super simple concept.
When you return something in a function, that function just holds the value that it returned (Kinda like a variable would).
For example, if you did this:
local function Add() --Create the function return 2+2 end local solution = Add() --Now we call it, it runs, and returns 2+2. That solution is 4. print(solution) -->>Output: 4
There are tons of other things you can do with returning. The basic idea though is that when you return something, the function holds that value. No code will run after something is returned.
For example:
local function func() print("Hey") return print("Heyyy") end --Expected output: Hey
The second "Heyyy" wouldnt print, because the function ends at return. Im also pretty sure Lua would throw an error if you tried to run code after a return, but don't quote me on that.
I'm sure there's someone that can explain it a LOT better than I can internally, because I'm not that good at knowing what goes on under the hood of it all.
In conclusion, when you return something within a function, the function basically takes that values reference in its place, sorta like a variable. You can also use returns as a similar solution to using break
with loops and such, since no code runs after you return something.