I've begun my attempt to understand the Lua PIL...

And i often run into very small confusions, if anyone wants to be annoyed by small questions over Steam, you can add me.

I’ll probably ask you the dumbest questions, but i can promise it won’t be after atleast an attempt of research.

I won’t give you 3 pages of code, to make you check why anything errors, or where i’m missing a bloody comma.

The LUA PIL leaves a lot of small things unexplained, and well, i don’t know any coding.

Mainly questions like ‘return n * fact(n-1)’
i think i understand that as "return to the function, factorial, times, func.factorial, brackets, n-1’ but i don’t understand why 1 is subtracted from the integer, or if that is what actually happens, because the description is simply factorial output of a number, not any subtraction involved.

add me on steam for a carpet bombardment of dumb confusions like the one above^^

This is more of a general programming/math thing and not just Lua, but the subtration is for recursion. Consider this:


function fact(n)
   if (n == 1) then
      return 1
   end
   
   return n * fact(n-1)
end

So fact(5) is 5 * 4 * 3 * 2 * 1 (5! in math notation), and the function does that accordingly.

So the bracket after the ‘fact’ function is the number? does ‘n-1’ signify a number or what?

[editline]14th August 2016[/editline]

OH, is it just to make the factorial function so it goes to the next integer, which is less than.

So instead of it breaking and stopping at 5*

it goes factorial -1 to let it go 5 * 4 * 3 * 2 * 1, and the code repeats in to get the sum

i am severely stupid, and this is probably false ^^^^

n is the argument which signifies the number. The first time, it’s the user inputted number; the other times, it’s the number provided by recursion. So if n is 5, you’d be calling:

fact(5) -> fact(4) -> fact(3) -> fact(2) -> fact(1), which results in 5 * 4 * 3 * 2 * 1

yeah, okay.


n * fact(n-1)

number * factorial(number -1)

i was confused by the minus one

but i see it’s to avoid it from going

5 * 5

and instead -1’s the number, so it gets going at 5 * 4 * 3 * 2 * 1

[editline]14th August 2016[/editline]

thx