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!
Let's say you have a function (Math function) and you want to calculate a number with it
1 | local function Add(x, y) |
2 | local TotalValue = x + y |
3 | end |
4 |
5 | print (Add( 1 , 2 )) -- prints nothing |
Now let's add a return statement to it
1 | local function Add(x, y) |
2 | local TotalValue = x + y |
3 | return TotalValue |
4 | end |
5 |
6 | print (Add( 1 , 2 )) -- prints 3 |
When you have function like this.
1 | function add(a, b) |
2 | return a + b |
3 | end |
what's that doing is adding that, and passing that variable to the line that called the function... For example
1 | function add(a, b) |
2 | return a + b |
3 | end |
4 |
5 | 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...
1 | function add(a, b) |
2 | return a + b |
3 | end |
and
1 | function add(a, b) |
2 | print (a + b) |
3 | 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
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:
01 | function hello() |
02 | print ( "hi" ) |
03 | print ( "hi" ) |
04 | return ( 10 ) |
05 | end |
06 | hello() |
07 |
08 | --[[ |
09 | OUTPUT: |
10 | hi |
11 | hi |
12 | nil |
13 | ]] |
this code will yield an error and return nil(nothing) because you didn't call the function to print 10
01 | function hello() |
02 |
03 | print ( "hi" ) |
04 |
05 | print ( "hi" ) |
06 |
07 | return ( 10 ) |
08 |
09 | end |
10 |
11 | print (hello()) |
12 |
13 | --[[ Output: |
14 |
15 | hi |
16 | hi |
17 | 10 |
18 |
19 | ]] -- |
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