Recursion Md. Imran Hossain Showrov (showrovsworld@gmail.com) 12 1
Outline  What is Recursion?  How Recursion works?  Examples of Recursion  Advantages and Disadvantages of Recursion
What is Recursion?  A function that calls itself is known as a recursive function.This technique is known as recursion.
How Recursion works? void recurse() { ... .. ... recurse(); ... .. ... } int main() { ... .. ... recurse(); ... .. ... }
How Recursion works? (cont..)
How Recursion works? (cont..)  The recursion continues until some condition is met to prevent it.  To prevent infinite recursion, if...else statement (or similar approach) can be used where one branch makes the recursive call and other doesn't.
Recursion: Example1 // Find the Sum of Natural Numbers using Recursion #include <stdio.h> int sum(int n); // function prototype int main() { int number, result; printf("Enter a positive integer: "); scanf("%d", &number); result = sum(number); printf("sum = %d", result); }
Recursion: Example1 (continue) int sum(int num) { if (num!=0) return num + sum(num-1); // sum() function calls itself else return num; } Output: Enter a positive integer:3 sum = 6
Recursion: Example1 (continue)
Recursion: Example2 // Find Factorial of a Number Using Recursion #include <stdio.h> long int multiplyNumbers(int n); int main() { int n; printf("Enter a positive integer: "); scanf("%d", &n); printf("Factorial of %d = %ld", n, multiplyNumbers(n)); return 0; }
Recursion: Example2 (cont..) long int multiplyNumbers(int n) { if (n >= 1) return n * multiplyNumbers(n-1); else return 1; } Output: Enter a positive integer: 6 Factorial of 6 = 720
Recursion: Example2 explaination  Suppose the user entered 6.  Initially, the multiplyNumbers() is called from the main() function with 6 passed as an argument.  Then, 5 is passed to the multiplyNumbers() function from the same function (recursive call). In each recursive call, the value of argument n is decreased by 1.  When the value of n is less than 1, there is no recursive call.
Recursion: Example2 (explanation)  Suppose the user entered 6.  Initially, the multiplyNumbers() is called from the main() function with 6 passed as an argument.  Then, 5 is passed to the multiplyNumbers() function from the same function (recursive call). In each recursive call, the value of argument n is decreased by 1.  When the value of n is less than 1, there is no recursive call.
Recursion: Example3 // GCD of Two Numbers using Recursion #include <stdio.h> int hcf(int n1, int n2); int main() { int n1, n2; printf("Enter two positive integers: "); scanf("%d %d", &n1, &n2); printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1,n2)); return 0; }
Recursion: Example3 (cont…) int hcf(int n1, int n2) { if (n2 != 0) return hcf(n2, n1%n2); else return n1; } Output: Enter two positive integers: 366 60 G.C.D of 366 and 60 is 6.
Advantages and Disadvantages of Recursion Advantages: 1. Reduce unnecessary calling of function. 2. Through Recursion one can Solve problems in easy way while its iterative solution is very big and complex. Disadvantages: 3. Recursive solution is always logical and it is very difficult to trace. (debug and understand). 4. In recursive we must have an if statement somewhere to force the function to return without the recursive call being executed, otherwise the function will never return. 5. Recursion takes a lot of stack space, usually not considerable when the program is small and running on a PC. 6. Recursion uses more processor time.
Lecture 12 - Recursion

Lecture 12 - Recursion

  • 1.
    Recursion Md. Imran HossainShowrov (showrovsworld@gmail.com) 12 1
  • 2.
    Outline  What isRecursion?  How Recursion works?  Examples of Recursion  Advantages and Disadvantages of Recursion
  • 3.
    What is Recursion? A function that calls itself is known as a recursive function.This technique is known as recursion.
  • 4.
    How Recursion works? voidrecurse() { ... .. ... recurse(); ... .. ... } int main() { ... .. ... recurse(); ... .. ... }
  • 5.
  • 6.
    How Recursion works?(cont..)  The recursion continues until some condition is met to prevent it.  To prevent infinite recursion, if...else statement (or similar approach) can be used where one branch makes the recursive call and other doesn't.
  • 7.
    Recursion: Example1 // Findthe Sum of Natural Numbers using Recursion #include <stdio.h> int sum(int n); // function prototype int main() { int number, result; printf("Enter a positive integer: "); scanf("%d", &number); result = sum(number); printf("sum = %d", result); }
  • 8.
    Recursion: Example1 (continue) intsum(int num) { if (num!=0) return num + sum(num-1); // sum() function calls itself else return num; } Output: Enter a positive integer:3 sum = 6
  • 9.
  • 10.
    Recursion: Example2 // FindFactorial of a Number Using Recursion #include <stdio.h> long int multiplyNumbers(int n); int main() { int n; printf("Enter a positive integer: "); scanf("%d", &n); printf("Factorial of %d = %ld", n, multiplyNumbers(n)); return 0; }
  • 11.
    Recursion: Example2 (cont..) longint multiplyNumbers(int n) { if (n >= 1) return n * multiplyNumbers(n-1); else return 1; } Output: Enter a positive integer: 6 Factorial of 6 = 720
  • 12.
    Recursion: Example2 explaination Suppose the user entered 6.  Initially, the multiplyNumbers() is called from the main() function with 6 passed as an argument.  Then, 5 is passed to the multiplyNumbers() function from the same function (recursive call). In each recursive call, the value of argument n is decreased by 1.  When the value of n is less than 1, there is no recursive call.
  • 13.
    Recursion: Example2 (explanation) Suppose the user entered 6.  Initially, the multiplyNumbers() is called from the main() function with 6 passed as an argument.  Then, 5 is passed to the multiplyNumbers() function from the same function (recursive call). In each recursive call, the value of argument n is decreased by 1.  When the value of n is less than 1, there is no recursive call.
  • 14.
    Recursion: Example3 // GCDof Two Numbers using Recursion #include <stdio.h> int hcf(int n1, int n2); int main() { int n1, n2; printf("Enter two positive integers: "); scanf("%d %d", &n1, &n2); printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1,n2)); return 0; }
  • 15.
    Recursion: Example3 (cont…) inthcf(int n1, int n2) { if (n2 != 0) return hcf(n2, n1%n2); else return n1; } Output: Enter two positive integers: 366 60 G.C.D of 366 and 60 is 6.
  • 16.
    Advantages and Disadvantagesof Recursion Advantages: 1. Reduce unnecessary calling of function. 2. Through Recursion one can Solve problems in easy way while its iterative solution is very big and complex. Disadvantages: 3. Recursive solution is always logical and it is very difficult to trace. (debug and understand). 4. In recursive we must have an if statement somewhere to force the function to return without the recursive call being executed, otherwise the function will never return. 5. Recursion takes a lot of stack space, usually not considerable when the program is small and running on a PC. 6. Recursion uses more processor time.