1 / 25

What Is Recursion? | Recursion Explained | Recursion Tutorial | Data Structures

In this Data Structures presentation, we will understand What Is Recursion in programming? with the help of an example. This video will acquaint you with a clear understanding of how recursion works in the memory area. Further, this Recursion Explained video will cover the coding implementation of one basic recursive problem.<br>So, let's get started!<br><br>1. What Is Recursion?<br>2. How Recursion Occurs in Memory Area?<br>3. Quiz<br>4. Different Types of Recursion<br>5. Program to Demonstrate Recursion<br>

Simplilearn
Télécharger la présentation

What Is Recursion? | Recursion Explained | Recursion Tutorial | Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. What’s in It For You? What Is Recursion? How Recursion Occurs in Memory Area? Different Types of Recursion Program to Demonstrate Recursion

  2. What Is Recursion?

  3. Click here to watch the video

  4. What is Recursion? • Recursion is the process through which a function calls itself directly or indirectly again and again. • Recursive function is defined in terms of base cases and recursive steps. • Base Case: Simplest Instance of problem. • Recursive step: Breaks problem into small instances using recursive calls.

  5. Mathematical Interpretation Problem Statement: Print sum of n natural numbers using Recursion. F(n) = 1 + 2 + 3 + 4 + ………….. + (n-1) + n Recursive step F(n) = n + F(n-1)

  6. Mathematical Interpretation Problem Statement: Print sum of n natural numbers using Recursion. Base Case Recursive step If(n == 0) { Return; } F(n) = n + F(n-1)

  7. How Recursion Occurs in Memory Area?

  8. How Recursion Occurs? Memory Stack Recursive Program in C #include<stdio.h> void fun(int a) { if (a == 0) return; printf(“%d”,a); fun(a-1); } int main() { int a =3; fun(a); } int fun(0) int fun(1) int fun(2) //Base Case ACTIVATION //Recursive step int fun(3) int main() int a

  9. How Recursion Occurs? Memory Stack Recursive Program in C #include<stdio.h> void fun(int a) { if (a == 0) return; printf(“%d”,a); fun(a-1); } int main() { int a =5; fun(a); } int fun(0) int fun(1) int fun(2) //Base Case //Recursive step int fun(3) Output 3 2 1 int main() int a

  10. Think and Answer! Based on the properties of Recursion, Which of the following statement is right? Recursion is always better than iteration Recursion uses more memory compared to iteration Recursion uses less memory compared to iteration

  11. Different Types of Recursion

  12. Types of Recursion

  13. 1 DIRECT RECURSION

  14. Direct Recursion A function is called direct recursive if it calls itself in its body again. #include<stdio.h> int fun(int z) { fun(z-1); } int main() { int a =3; fun(a); } Recursive call in function body

  15. 2 INDIRECT RECURSION

  16. Indirect Recursion The recursion in which the function calls itself via another function is called as indirect recursion. #include<stdio.h> int fun1(int z) { fun2(z-1); } int main() { int a =3; fun1(a); } int fun2(int y) { fun1(y-2) }

  17. 3 TAILED RECURSION

  18. Tailed Recursion A function which executes a recursive call at the end of it’s local body is called as tailed recursive function. #include<stdio.h> int fun(int z) { printf(“%d”,z); fun(z-1); } int main() { int a =3; fun(a); } Recursive call at the end of function body

  19. 4 NON-TAILED RECURSION

  20. Non-Tailed Recursion A function which does not execute a recursive call at the end it’s local body is called as tailed recursive function. #include<stdio.h> int fun(int z) { fun(z-1); printf(“%d”,z); } int main() { int a =3; fun(a); } Recursive call is not the last execution of this function.

  21. Program to Demonstrate Recursion

  22. Program to Demonstrate Recursion Let’s implement a program for printing sum of n natural numbers using C programming language.

More Related