# Factorial

#1

I agree in that I do not understand how this is working. I did not think that if statements looped without being inside a while or for loop?

The way I read this if x = 8 this will produce 8*7 and that will be the end. What is making loop back to add the 6, 5, 4 etc.?

``````def factorial(x):
if x > 1:
result = x*factorial(x-1)
else:
return x
return result``````

Continuing the discussion from 5 factorial:

#2

Hi
I think that the best way to understand an iterative function is to have a look at the scan hereunder

The function is iterative because you are calling the function in its own code. So the function will be called until the moment where x=1. The variable `result` takes the values

``````result = 2 * 1 = 2    when x = 2
result = 3 * 2 = 6    when x = 3
result = 4 * 6 = 24   when x = 4
result = 5 * 24 = 120 when x = 5``````

Cheerio

#3

This is not iterative. It is recursive. `result` is redundant and not even needed. The way it is described above would have us believe that `result` is a value when it is not. It's just a mimic of the real call stack.

``````def factorial(x):
return x * factorial(x-1) if x > 1 else 1``````

#4

Oups indeed I inversed iterative and recursive. However, @sglen asked for an explanation on how a recursive function works. So I really think that the way I explained it is suited.
Cheerio

#5

It's not an explanation, but a farce. It doesn't explain anything.