General: Recursive Programming

So what is recursive programming?  As someone who took up Computer Science, this is something you learn in school but never really used it at work.  Unless you work in a specialized field, it is not something that one might use when programming business applications.

Remember Fibonacci?  Fibonacci numbers are defined as the sum of the two preceding numbers.

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, ...

I still remember Fibonacci because I was given a test on it in my job interview 4 years ago.  In fact, I blogged about it here.  You can use a loop function to program Fibonacci, but a recursive function is the way to go, as in below:

public int fibonacci(int n)
{
if (n <= 0) return 0;
else if (n == 1) return 1;
else return fibonacci(n - 1) + fibonacci(n - 2);
}

You can use recursive functions to implement linked lists and binary search as well.  Go to this link to quickly refresh yourself on recursive programming.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s