Lua is awesome because it has the ability to call a function inside itself. This is called recursion and is useful in instances where are large task can be broken down by repeatedly doing the same smaller task. This is seen in fractals and maybe FOR LOOPS!
So here's how you can use recursion to do a manual for loop.
Pretty sweet right?
All we do is pass on the original number and the if statement is there in order to prevent the function from counting endlessly upward. See if you can think of another situation this would be useful. Also, if you're curious for more, check out the wikipedia article because I've explained just about all I know about recursion.
No comments:
Post a Comment