HomeCoding & Programming

PHP Recursion and recursive routines

PHP Recursion and recursive routines
Like Tweet Pin it Share Share Email

Recursion and recursive routines is a bit more complicated, it can be defined as a regular function that calls itself.

 

Learning when and how to apply recursion is probably a lot more difficult than just learning what recursion is? and being able to explain recursion effectively in an interview situation is even more difficult one 🙂

 

For beginners defining recursion is easy – any function that calls itself is a recursive function. Isn’t it?

 

But do you know what is recursion used for?

 

Recursion is best used for the case where a large task can be broken down into a repetitive “sub-task”. Because a recursive routine calls itself to perform those sub-tasks.

 

Factorial is the best example to understand the recursive case.

function factorial($x)
{
  if($x > 1){
     /* recursive case */
     return factorial($x-1) * $x;
  }
  else  /* base case */
     return 1;
}

 

Where $x is the number whose factorial we want to find and that is what recursion is all about – finding repetitive patterns, and breaking a problem down into repetitive sub-tasks.

 

Comments (20)

  • My spouse and I stumbled over here unique web page and believed I should examine things out.

  • Wow that was odd. I just wrote an really long comment but after I clicked submit my comment didn’t appear. Grrrr… well I’m not writing all that over again. Regardless, just wanted to say excellent blog!

  • Have you ever considered about including a little bit more than just your articles? I mean, what you say is important and all. Nevertheless think about if you added some great pictures or video clips to give your posts more, “pop”! Your content is excellent but with pics and clips, this blog could definitely be one of the best in its niche. Good blog!

  • Well I truly enjoyed studying it. This article provided by you is very helpful for proper planning.

  • I’ve read several good stuff here. Definitely worth bookmarking for revisiting. I wonder how much effort you put to make such a excellent informative site.

  • I think this is one of the most vital info for me. And i’m glad reading your article. But want to remark on few general things, The website style is great, the articles is really excellent : D. Good job, cheers

  • You should take part in a contest for one of the best blogs on the web. I will recommend this site!

  • I found your weblog web site on google and examine a number of of your early posts. Continue to maintain up the very good operate. I just extra up your RSS feed to my MSN News Reader. Seeking forward to reading more from you later on!

  • Great work! This is the type of information that should be shared around the net. Shame on the search engines for not positioning this post higher! Come on over and visit my web site . Thanks =)

  • Nice blog here! Also your site loads up very fast! What host are you using? Can I get your affiliate link to your host? I wish my web site loaded up as quickly as yours lol

  • While there’s no problem with that, there are going to you should be some issues in the near future.

  • It comprises a wide range of procedures native to cultures around the world.

  • What’s up to every one, it’s in fact a good for me to go to see this web page, it includes important Information.

  • Wow, fantastic blog layout! How long have you been running a blog
    for? you made blogging look easy. The whole look of your site is
    great, let alone the content!

  • Your mode of explaining everything in this piece of
    writing is truly good, every one can simply understand it,
    Thanks a lot.

  • Oh my goodness! Impressive article dude! Thank you so much, However I am encountering issues with your RSS. I don’t know the reason why I can’t subscribe to it. Is there anybody having the same RSS problems? Anyone that knows the answer will you kindly respond? Thanx!!

  • I’ve been browsing on-line greater than 3 hours as of late, but I by no means found any interesting article like yours. It’s lovely price enough for me. In my view, if all webmasters and bloggers made just right content as you did, the net will probably be a lot more useful than ever before.

  • very good put up, i definitely love this web site, keep on it

  • Great article! That is the kind of info that should be shared
    around the web. Shame on Google for now not positioning this post higher!
    Come on over and seek advice from my web
    site . Thank you =)

Comments are closed.