5/15 factorial


#1

Hello, just started on python and I am confused about how the return function works.

Given Task:
Define a function factorial that takes an integer x as input.
Calculate and return the factorial of that number.

My initial function ( the - represents spaces that i used in my coding, the post doesnt seem to show them ):

def factorial(x):
–product = 1
–for i in range(x):
----product = product * (i+1)
----return product

After subbing x as various numbers, i realized that the result always seems to be 1 and i am unsure why

After correction:

def factorial(x):
–product = 1
–for i in range(x):
----product = product * (i+1)
–return product

By bringing the return function out of the for function, it seemed to work.
Can someone help explain the meaning of the return function and how it led to the answer always being 1 in my initial function. Thank you!! :))


#2

Well with Python, the whitespace for proper structuring to your code is key. Here your return function was improperly spaced to which should have caused an error.

Now I am also curious on this being just having broken the surface with python… so someone with more knowledge will have to correct me I am sure…

but my first thought is because of the spacing the code and the fact it was inside your for loop was read as if you are returning the product of your product = 1 therefore why your product no matter how many times you changed your for loop was equal to 1. However, once properly spaced and the return statement was put outside your for loop, the code read your return function in correspondence to your for statement making the product it returned come from your for statement.

Again I am wanting explanation on this too so if I am wrong correct me haha


#3

for function? For loop you mean

well, by default a function returns None at the end of the function

if we want to return something else at the end of the function, we can use the return keyword.

So when a return keyword is reached, the function ends

so by placing return in the for loop, the function ends in the first iteration of the loop (return will break the loop so the function can end), this will cause an incorrect factorial calculation


#4

Thank you that is what I meant. So basically, because the return statement ends in the for loop stopping the code, the for loop never runs thus causing the return statement to return the product = 1 each time? due to that being the only valid product available to return?

And I have heard you say these types of things a million times @stetim94 haha but I guess I just never stopped to think about what it exactly meant to closely… I always just attributed it more to its the formatting of your code rather than necessarily it being the function of your return statement and what it does in the code… guess my dense brain needs a loosing hehe well i won’t forget it now xD


#5

BTW for future reference, if you want your indentation to show up in the forums, all you have to do is format your code…


#6

Well, the for loop runs, but in the first iteration of the loop the return keyword is reached, so the function ends

well, product is returned. The value of returned is 1, given the value of i in the first iteration is 0, so product * (0 + 1). which results in 1, so 1 is returned


#7

Ahhhh ok gotcha. Interesting. So when the return statement ends in the for loop it doesn’t make the for loop invalid just ends the code after having run everything previous including that which was in the for loop before it… nice. Assuming that thought process is correct…


#8

yes, by default functions return None at the end of the function:

def example():
    print "None is being returned at the end of function"

print example()

by putting print statement before function call, the returned result is printed.

and we can prove code before return statement is executed, and not after the return statement:

def example():
    print "prove of concept"
    return None
    print "see?"

example()

#9

Ohhh okay I understand now thank you!!! :slight_smile:


#10

I see I am new to this, thank you very much!


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.