Recursive function in C++
Welcome to Virtual Study Solutions C++ tutorials. Today we will discuss and learn about Recursive function in C++ Programming language.Topics we will cover in this Tutorial will be:
- What is a Recursive function ?
- Recursive function important parts
- Recursive function example code
- How it affects stack Recursion in c++ programming.
What is Recursive function in c++ Programming |
What is Recursive function
First of All You will be thinking What is recursion or Recursive function? The simple answer to Your Question is, it’s when a function calls itself. and to be Precise we can define Recursive function as:
A function calls it self until a certain condition is true.As You may know Recursive function is one of the most annoying and difficult concept for c++ programming for beginners. Yes it is difficult for a beginner to understand because sometimes its execution goes in depth where things becomes complicated and a beginners feel difficult to understand it.
Also Read: How to Write C++ Program to Find Prime Number
Recursive function important parts
A recursive function has two important parts
- Base case, Stopping state, stopping condition
- Functions call its self with a specific conditions (Recalling condition)
Also Read: What is Recursive function in c++ with Examples
Lets write a simple example to understand the how recursive function works.
In this example an integer 'n' is passing to a function in which its value is incremented by one in recalling condition.
When its value becomes 11 or greater than 10 (base case) it returns all calls.
Also Read: How to write C++ Program to find factorial of Number
using namespace std;
void recursive_function(int n){
if(n>10) //base case
return;
else{
cout<<"Recursive Function call number "<<n<<endl;
recursive_function(n=n+1); // here function is calling it self
}
}
int main(){
int n=1;
recursive_function(n); // function call
return 0;
}
Now Lets have a look to this program it has a problem.
#include<iostream>
using namespace std;
void recursive_function(){
cout<<"I am Recursive Function"<<endl;
recursive_function(); // here function is calling it self
}
int main(){
recursive_function(); // function call
return 0;
}
When a function is called it puts on to the stack. In the above program function is calling itself again and again and putting its calls on the stack again and again where stack is reserving some resources for each call.
So the stacks are not in large size and when stack becomes full, our program get crashes. This is why because there is not a base case in program where function will stop putting itself on stack.
Also Read: How to write C++ program to find Fibonacci Series
It is recommended to understand the concept do experiment with the code and analyze the output and also dry run the code on paper.
Recursive function Base Case:
Its a condition when it becomes true or executes function stop to call it selfRecursive function Recalling Condition:
A condition in which function recalls it self with a specific parameters or it depends on the required result we want to get.Lets write a simple example to understand the how recursive function works.
In this example an integer 'n' is passing to a function in which its value is incremented by one in recalling condition.
When its value becomes 11 or greater than 10 (base case) it returns all calls.
Also Read: How to write C++ Program to find factorial of Number
Recursive function Example Code
#include<iostream>using namespace std;
void recursive_function(int n){
if(n>10) //base case
return;
else{
cout<<"Recursive Function call number "<<n<<endl;
recursive_function(n=n+1); // here function is calling it self
}
}
int main(){
int n=1;
recursive_function(n); // function call
return 0;
}
Recursive function Code output:
Here is the Output of above example code of Recursive function.Now Lets have a look to this program it has a problem.
#include<iostream>
using namespace std;
void recursive_function(){
cout<<"I am Recursive Function"<<endl;
recursive_function(); // here function is calling it self
}
int main(){
recursive_function(); // function call
return 0;
}
How it Affects stack Recursion in c++
You must be wondering when you run the above code our program get crashes why? The Answer of this Question is very Simple.When a function is called it puts on to the stack. In the above program function is calling itself again and again and putting its calls on the stack again and again where stack is reserving some resources for each call.
So the stacks are not in large size and when stack becomes full, our program get crashes. This is why because there is not a base case in program where function will stop putting itself on stack.
Also Read: How to write C++ program to find Fibonacci Series
It is recommended to understand the concept do experiment with the code and analyze the output and also dry run the code on paper.
Tags: Recursive function, Recursive function in c++, Recursive function in cplusplus, Recursive function in c++ programming, c++, c++ tutorials, How to write C++ Program, Programming in C++, VU, Virtual University.
You should Read More Examples to better understand Recursive function.
Recursive Linear Search Function c++ example
Recursive Linear Search Function c++ example
Post a Comment