Hi all,
Well hopefully you'll forgive my dumbness to lua for the time being and are understanding that to learn i have to start somewhere. Having played wow for a fair few years now and getting tired of my favourite addons getting broken and stopped being updated, plus the fact i can never find the addon i would like lol. I have decided to broaden my horizon and learn lua through your book.
Now for the dumb questions to start if nobody here minds that is. I like to understand what i am learning (doesnt make sense but bare with me plz).
In chapter 2 the code calculating the factorial of a number (below) i have been trying to break it down so i can understand it better to follow. Whether this is a good idea or not i dont know but i was having trouble following the book further in so thought i'd go from the start again. Anyway :
Code:
function fac(n)
if n == 0 then
return 1 -- so by my thinking the first time this part is ran n will always be 0
else
return n * fac(n - 1) -- from the for part of this code further down when the code is looped here after i.e 2nd time function fac(n) is ran it will be 1 because of the return 1 in the 3rd line and the return changes to the outcome of n *fac(n - 1)
end
end
for i = 1, 10 do
print(fac(i)) -- so this runs the loop 10 time altering (n) as it is called
If i try and make this code down on paper and follow it i cant make the same numbers as the code does, the only thing i can think of is (i) is being inserted in the code somewhere to alter the value of (n) depending on the 1, 10 part it's at. Basically can someone explain the last 2 lines for me plz.
As i said im still learning and may be looking at this all the wrong way but it's always good to bounce stuff off other peoples heads instead of giving up. I have a reference book on it's way from amazon lol hoping it will help.
If anyone could take the time to help me learn with the dumb q's i have it would be most appreciative.
Thanks