(I am Xapelize No. 2) Before understanding recursion, let's make you pro at functions, what really is a function...
Think of function as a group of some code stored somewhere in your computer. Your code runs line by line, say you have 10 lines, at first, line 1 will be executed, after that line 2, that's obvious, however, what if you wrote code that kills everyone on the server:
It takes 3 lines, good enough, but what if you wanted to use this piece of code again somewhere, absolutely same code without any changes, you could duplicate it:
5 | ... some more random code |
But that will make the code unnecessary complicated and longer than it should be. What instead you can do is take these 3 lines of code and put them into a group, or into a model, or into a folder, whatever you want to call it, it's usually (always) called function.
1 | create function named kill-all-players |
Say this will create the function, it will have these 3 lines as it's content, called function body. I named the function kill-all-players so we can use it later (if we created more functions we must somehow identify them, right). Next time we want to kill every player, instead of writing these 3 lines again, tell Roblox that you already did that, you give Roblox name of function and Roblox will look for it, when it finds it, it will see that it has 3 lines and execute them:
Now the code is easier to understand. What happened is that code literally jumped to that function:
1 | create function named kill-all-players |
In Roblox the code would actually look like this:
1 | local function kill_all_players() |
But since you can execute this kill_all_players function anywhere, what if you were to execute it in the function body itself, like this:
1 | local function kill_all_players() |
Just this is already called recursion, function executed itself. What would happen is that first Roblox would create a function kill_all_players, then at line 7 it would jump at line 2 (start of kill_all_players's function body) and then proceed to line 3, then it would jump at line 2 again since that's how function execution works, then it would proceed to line 3 again, then at line 3 it would jump at line 2 again, then it would proceed to line 3 again, then again jump to line 2 and so on infinitely.
So far we created while true do loop, useless and will just break the code. But maybe you could create a counter variable to prevent infinite loop:
03 | local function kill_all_players() |
07 | print ( "I got called for the " .. counter .. " time!" ) |
What happens is that at line 15 the code jumps at line 4, proceeds to line 5 and increases counter (which is 0) by 1, then it prints something, after that it checks if the counter is lower than 10, indeed, 1 is lower than 10, so, it runs on line 10 which executes the same function, thus it goes to line 4 again and proceeds to line 5, increases counter by 1 again and prints it, now it's 2, but 2 is still lower than 10 so it repeats this. When counter hits number 10, then
is no longer executed and thus the function hits line 12 at which it goes back to line 16.
Practical use case might be when you want to display table, say you have table like this:
Such table has random numbers in it, but, it also has so called nested tables, these are another tables with numbers in it. If you would want to print it, you'd need to get all the numbers from this, using recursion the logic would be the following:
1) Loop through elements on first layer first, these are a, b, c and c, b and c are numbers, print them
2) a and d are tables, what we can do is execute the same function which will go to step 1) and Loop through elements on the layer a, there it will see elements a, b and c, these are no longer tables so we can print them!
3) Do same for every nested table in nested table in nested table maybe
Or in code:
01 | local function print_table(super_table) |
03 | for key, value in pairs (super_table) do |
06 | if type (value) = = "table" then |
1239812938
234982394
4593284923
34892034234
43294879238
34829047882347823
423974823742348
23740237482374
4573284752839