Saturday, May 29, 2010

PHP Recursive Function

As a self-taught PHP developer with no training, I was recently embarrassed that I could not write a recursive function.



Recursive Function: A function that refers to itself.

Recursion: Something that refers to itself.



What is recursion for? I'll do my best to explain this. Often there are things that can be done either by iteration (like a simple loop that keeps adding one) or by recursion (its a loop, but it functions a little differently). True recursion always has an end or reaches a final value.



Here's a simple example of an 'iterative' loop. Let's say you're trying to find the factorial of 7, written as 7! (which means, 1*2*3*4*5*6*7). Here's an iterative loop to do this:




‹?php
function myfactorial($input) {
$result = 1;
while ($input > 0) { $result *= $input; $input--;}
return $result;
}
?›

now we can run the function:
(add this line after the function)
print myfactorial(7);
the result we get is 5040



There's another way to calculate the factorial. It is a loop, but it's recursive, since the function refers to itself. Effectively we keep multiplying the function total smaller input numbers until we reach 1, then we stop. In other words, myfactorial(7) starts at a value of 7, which we multiply by 6, then we multiply the result by 5, then by 4, etc. This does the same thing as the function above, but it does it recursively




‹?php
function myfactorial($input)
{
if ($input < 2) {return 1;}
else {return ($input * myfactorial($input-1));}
}
?›

Now we can execute this function:
(add this line after the function)
print myfactorial(7);

the result we get is 5040



So what good is recursion in real life? I am still working on that. Some say the idea of recursion somewhat references 'divide and conquer'. Basically, recursion represents an algorithm... perhaps even a very simple algorithm. Recursion can be used to break down complex problems down to a simple common denominator. Once you remove all the simple instances of an algorithm from a complex problem, sometimes you are left with something very small and simple. Apparently the programming language LISP is tuned for recursion, and apparently most functions in that language use recursion...

2 comments:

  1. This link gives some better examples of recursion: http://devzone.zend.com/article/1235

    Another place you might want to take a look is at wikipedia:
    http://en.wikipedia.org/wiki/Recursion

    One thing I found pretty amusing was if you search for "recursion" on google, it will say "did you mean recursion". It's playing on the fact that recursion always refers to itself.

    ReplyDelete
  2. recursive functions are really useful when you are trying to store data in memory instead of into a hard drive or database (etc. link-list, vtable, memory trees...)..I use them in my programming class, however in real life, I only done it when I want to store and search large data in RAM versus disk i/o for performance reason. Other than that, I have used it much (business apps). I'm sure system programmers use this more often

    ReplyDelete