Author Topic: Recursive function  (Read 2442 times)

0 Members and 1 Guest are viewing this topic.

Offline runeazn

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 191
  • Rating: +5/-3
    • View Profile
Recursive function
« on: January 27, 2012, 03:53:18 pm »
So im followign a guide but i dont  get it D:

let say we have

def factorial(number):
    if number <=1 #base case
        return=1
    else:
        return number*factorial(number-1)




the return 1 part wont it keep continue repeating the process of putting 1 in the factorial to infinty?

or am i reading it wrong?

or if it returns 1 then that means no matter what factorial(x)= 1
??

Offline turiqwalrus

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 840
  • Rating: +51/-2
  • Wheeeeeee~!
    • View Profile
Re: Recursive function
« Reply #1 on: January 27, 2012, 03:59:23 pm »
"return=1" exits out of the function and displays the number 1 IIRC

and the other return:
return number*factorial(number-1)

using an example of number=two:

    if number <=1 #base case   //2>1, therefore it ignores this statement
        return=1
    else:                                     //else...
        return number*factorial(number-1)   // it returns 2*factorial(number-1)   //2*1
                                                       

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Recursive function
« Reply #2 on: January 27, 2012, 04:03:00 pm »
Lets break it down, lets see what happens when we try factorial(3).  

Code: [Select]
We call factorial(3), and since 3 is larger than 1, it returns 3*factorial(3-1).
but before we get an answer we need to calculate the factorial of 3-1 (2).  Lets do that.

Code: [Select]
we call factorial(2), and since 2 is larger than 1, it returns 2*factorial(2-1).
So now we need to calculate the factorial of 2-1 (1).  This would be

Code: [Select]
We call factorial(1), and since 1 is equal to 1, it returns 1.
So moving backwards, we have:
Code: [Select]
factorial(1) = 1;
factorial(2) = 2*factorial(1) = 2*1;
factorial(3) = 3*factorial(2) = 3*2*factorial(1) = 3*2*1;

Make a bit more sense?  This is the way recursive functions work, they call themselves multiple times with different numbers to get a final answer.
« Last Edit: January 27, 2012, 04:05:02 pm by Builderboy »

Offline runeazn

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 191
  • Rating: +5/-3
    • View Profile
Re: Recursive function
« Reply #3 on: January 27, 2012, 04:08:28 pm »
i dont see anywhere it will fill in the 1?

or will it automatically do when returning the 1 ,

number =1?

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Recursive function
« Reply #4 on: January 27, 2012, 04:10:21 pm »
I'm not quite sure I understand your question?  When number is 1, the If statement is true and therefore the method simply returns 1.  When the number is not 1, it returns the current number multiplied by the factorial of the next lowest number.

Offline runeazn

  • LV4 Regular (Next: 200)
  • ****
  • Posts: 191
  • Rating: +5/-3
    • View Profile
Re: Recursive function
« Reply #5 on: January 27, 2012, 04:18:42 pm »
it returns 1 but where does it return the 1 to?
to the number so it can go into the factorial or does it return 1 to i dunno?

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Recursive function
« Reply #6 on: January 27, 2012, 04:20:08 pm »
It returns the 1 to wherever called it.  That location could either be inside the function itself, or somewhere else.  If I call factorial(1), it returns the 1 to me, but if I call factorial(2) it returns the 1 to the first factorial() method.