Recursion in C++ by R4R Team

Recursion:
-Function call itself again and again is known as the recursion.
-We can create a loop type structure with the help of the recursion.
-recursion require a stack memory for execution.

Infinite loop by recursion:
program-

#include< stdio.h>
int main()
{
main();
}

-Here when execution starts then it will go in main() function then we will again call main() function and this process continue run.

How recursion work in C++:

program:

#include < iostream>
using namespace std;
class Myclass
{
public:
int func(int i)
{
if(i==0)
return 0;
cout<< i<< endl;
func(i-1);
}
};
int main()
{
Myclass obj;
obj.func(5);
return 0;
}


output-

5
4
3
2
1

-In this program, we call a function func() with starting value of i is 5 then call function again and again with decrease value of i and if value of i will become 0 then return.




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!