recur8 - Recursion Chapter 4 Introduction Recursion is a...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction Recursion is a important and powerful tool in problem solving and programming solutions to problems. It is a programming/algorithmic technique that naturally implements the divide-and-conquer problem solving methodology. Some programming languages, such as Lisp, are inherently recursive languages while others, such as C, support the concept of recursion. Recursion is simply a function which invokes itself either directly or indirectly. Recursion is an alternative to iteration although care must be taken as often recursion is not an efficient alternative to iteration. In many situations, the problem itself is expressed or defined in recursive terms and it becomes natural to develop solutions to that problem in the same recursive terms and hence a recursive program is developed. There are a couple of different forms that recursion can assume in the context of programming algorithms and we will discuss these differences later. For now, we’ll simply look at recursion in general and how to follow the actions of a recursive function. Recursion in its Simple Form Consider the program shown below: What does this program do? It simply calls the function count_down with an initial parameter value of 10. The function count_down calls itself as long as the Recursion - 1 Recursion – Chapter 4 void count_down (int n) { if (n) { printf(“%d! “, n); count_down(n-1); } else printf(“\n Blast Off!\n”); } int main ( ) { count_down(10); }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
value of n is not 0. Notice however, that it calls itself with a different value of n each time! This is an important aspect of recursion to which we must adhere or the recursion might never end and we would have an infinite loop in the code. We’ll return to this later. Consider the following definition for the multiplication of two natural numbers:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 8

recur8 - Recursion Chapter 4 Introduction Recursion is a...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online