python recursion, example in php

I am having trouble understanding recursion in python because I can not relate to php (a language I am more familiar)

http://www.experts-exchange.com/Programming/Languages/Scripting/Python/Q_28489785.html
LVL 1
rgb192Asked:
Who is Participating?
 
aikimarkCommented:
How about calculating the factorial of a number?  This is the product of all the integers between that number and 1.
Example:
5! = 5*4*3*2*1 = 120
A PHP example of a recursive function that calculates the factorial of the number passed to it:
function fact($number) { 
    if ($number === 0) { 
        return 1; 
    } 
    else { 
        return ($number * fact($number-1)); 
    } 
}

Open in new window

Once we get down to 1 we will always be multiplying by 1, so we might be a bit more efficient if we write it as
function fact($number) { 
    if ($number < 2) { 
        return 1; 
    } 
    else { 
        return ($number * fact($number-1)); 
    } 
}

Open in new window

In both cases, we have some conditional test that stops the recursion.
0
 
aikimarkCommented:
how would you generate an Nth Fibonacci number in PHP (with or without recursion)?
0
 
rgb192Author Commented:
how would you generate an Nth Fibonacci number in PHP (with or without recursion)?

I do not know.
0
 
rgb192Author Commented:
thanks.

recursion example works
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.