




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to recursion, a programming technique used to solve problems through the repeated application of a function on smaller instances of the same problem. The concept of recursion, its differences from iteration, and provides examples of recursive functions in c. It also discusses the importance of base cases and the role of a stack in recursive function execution.
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





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:
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); }
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 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: a * b = a added to itself b times. Notice that this is an iterative definition of multiplication. Now consider an alternative definition for multiplication: a * b = a if b = 1 a * b = [a * (b – 1)] + a if b > 1 Notice that this is a recursive definition. If we have two numbers a = 3 and b = 3, then the second line of the definition applies and we have: 3 * 3 = [3 * (3 – 1 )] + 3 = 3 * 2 + 3 =? Once again, since b in this case is greater than 1, the second line of the definition applies and we have: 3 * 2 = [3 * (2 – 1)] + 3 = 3 * 1 + 3 =? Finally, since b = 1 this time, we have the first line of the definition to apply and we produce our result as: 3 * 1 = 3 Since we have now produced an answer for the value of 31 we know that the answer for the problem 32 is (31) + 3 = 6. Since we have produced the answer for 32, we know that the answer for 33 is (32) + 3 = 6 + 3 = 9. As another example of a naturally occurring recursive problem, consider the factorial function which is recursively defined as: Practice Problem 1: (answer on page 8 ) Write a C function that will implement this recursive definition of multiplication.
x = multiply(6, 3); Example A palindrome is a string of characters that reads the same forwards and backwards. Example palindromes are: level, deed, mom, and dad. Let’s construct a function that will allow us to determine if a string of characters is a palindrome or not by printing the characters in the reverse order. Recursion - (^4) m = 6 n = 3 3 <= 1? FALSE return (6 + multiply(6, 2) m = 6 n = 2 2 <= 1? FALSE return (6 + multiply(6, 1) m = 6 n = 1 1 <= 1? TRUE return (6) void palindrome (int n) { char next; if (n <= 1) /* base case / { scanf(“%c”, &next); prinf(“%c”, next); } else { scanf(“%c”, &next); palidrome(n-1); printf(“%c”, next); } return; }/end palindrome / int main ( ) { printf(“Enter a string: “); palindrome(5); /assume 5 character strings */ printf(“\n”); }
Trace of palindrome if input string is abc : palindrome(3); n = 3; 3 <= 1? FALSE read next character: a palindrome (2) write a return n = 2; 2 <= 1? FALSE read next character: b palindrome (1) write b return n = 1; 1 <= 1? TRUE read next character: c write c return Practice Problem 3: (answer on page 8 ) Write a C function that will recursively determine an Fibonacci number. The Fibonacci numbers are defined by: fib(n) = n if n <= 1 fib(n) = fib(n-2) + fib(n-1) if n >= 2
The C version of the algorithm void tower (char From, char To, char Temp, int N) { if (N == 1) printf(“Move disk 1 from tower %c to tower %c\n”, From, To); else { tower(From, Temp, To, N-1); printf(“Move disk %d from tower %c to tower %c \n”, N, From, To); tower(Temp, To, From, N-1); } return; } To test this function call is as: tower(‘A’, ‘B’, ‘C’, 3)
Answer to Practice Problem # Answer to Practice Problem # Answer to Practice Problem # int multiply (int m, int n) { if (n == 1) /* terminating case / return m; else return (m + multiply (m , n-1)); } int factorial (int n) { if (n <= 0) / terminating case / return 1; else return (n * factorial (n-1)); } int fibonacci (int n) { if (n <= 1) / terminating case */ return n; else return (fibonacci(n-2) + fibonacci(n-1)); }