240 likes | 254 Views
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>
E N D
What’s in It For You? What Is Recursion? How Recursion Occurs in Memory Area? Different Types of Recursion Program to Demonstrate Recursion
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.
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)
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)
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
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
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
1 DIRECT RECURSION
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
2 INDIRECT RECURSION
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) }
3 TAILED RECURSION
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
4 NON-TAILED RECURSION
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.
Program to Demonstrate Recursion Let’s implement a program for printing sum of n natural numbers using C programming language.