# Factorial function? what?

<Below this line, add a link to the EXACT exercise that you are stuck at.>
<In what way does your code behave incorrectly? Include ALL error messages.>

The instructions say:
Define a function factorial that takes an integer x as input.

Calculate and return the factorial of that number.

I have been reading the forums and I understand the solution (even though I am curious about the aim of this exercise since I don’t recall having learn the factorial function before in previous exercises), but I am very puzzled about something. The forums say that factorial is a recursive function, which I take to mean something like it will automatically repeat itself or something. But I don’t understand…why can’t we just take factorial(x) straightaway, where x is a number? if factorial is a function recognised by Python why can’t we just use it directly?

Many thanks Factorial of x can be expressed as `x * factorial(x-1)`, which means that the function calls itself recursively. (and factorial(0) is defined as 1)

I understand what that means, and I also learnt in Math in school that 0! = 1. But I don’t understand why you can’t apply it directly since ultimately you are still having to use the factorial function directly (if you don’t do the iteration method)

You don’t have to use `math.factorial` to write a recursive factorial function, no.

Just a single function that takes one argument and returns one value. That function calls itself recursively.

woops, I don’t quite mean that either. I mean, why can’t you do this directly:

def factorial(x):
return factorial(x)

Thanks for helping That would just keep calling itself until Python refuses to keep track of any more call stacks. Try it.

It will not return.

yup I tried it and got an error message, which is why I am trying to find out more about why it doesn’t work by asking here on this forum.

I was essentially wondering since you are going to be using the factorial function eventually anyway when doing x * factorial(x - 1), why can’t I use it directly with factorial(x) ?

Sorry if this is an ultra beginner question, because that is precisely what I am If a function just calls itself over and over then it will not terminate, that’s why

If it doesn’t terminate you won’t ever get a result.

If you do x-1 untill it reaches 0 and then return 1, then it terminates, and you’ve got a result.

``````def factorial(x):
return factorial(x)
``````

That’s just an infinite loop, there’s nothing there that does any computing.

1 Like

ah I see But how would a code like this which works (with the correct indentation):

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

know when to terminate the function? wouldn’t it loop infinitely as well?

1 Like

Try executing it manually?

And strictly speaking that doesn’t run. Your indentation is all messed up in your post.

Okay so:

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

factorial(5) #will return 5 * 4!
factorial(4) #will return 4 * 3!
factorial(3) #will return 3 * 2!
factorial(2) #will return 2 * 1!
factorial(1) #will return 1
factorial(0) #will return 0 * (-1)! -> incorrect

so how does it know when to stop?

With the comment in the brackets I meant given that indentations are correct. I know it is wrong in my reply, but I forgot how to do it (I could do it once a few weeks back) and I can’t find how. I thought it was triple quotation marks with [python] but it didn’t work so yeah messy indentation sorry

It stops when it returns without calling itself first.

And yes, it should handle 0 so your version is slightly incorrect.

1 Like

You are almost there. Look at the line before the return x and compare it to your last line.

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

VBR Patrick

2 Likes

I think i know what you meant, and i think i managed to solve it: #v2 means version2

def factorial_v2(x):
#indentation result = factorial(x)
#indentation return result

# test examples

print factorial_v2(3) #prints 6
print factorial_v2(0) #prints 1
print factorial_v2(1) #prints 1

so looks like you can just use a built in FACTORIAL function, without even telling it that factorial 1 is 1, the only thing is you first have to define your own function and inside this function first ask to store the result in a variable and then return the result of built-in FACTORIAL function, if that makes any sense…?

Are you sure your code works? I just tested it… hmmm… this is just bizzare…

i just pasted only this code and its a fact, it doesnt work on its own, however when i had it written down below the first version of my factorial it did work:

def factorial(x):
if x == 1 or x == 0:
f = 1
return f
else:
f = x * factorial(x - 1)
return f

# test examples

print factorial(3) # prints 6
print factorial(4) # prints 24
print factorial(0) # prints 1

def factorial_v2(x):
result = factorial(x)
return result
print factorial_v2(3)
print factorial_v2(0)
print factorial_v2(1)

so it must be that factorial_v2 is using the first factorial function which is useless…

yup and I was just wondering why. That was my original question Anyway 2 days ago I realised I accidentally skipped some exercises and jumped to this so I am leaving this alone for now and doing what I haven’t done previously and hope that I will understand later on that’s won’t be following the instructions, that’s all 