I am trying to work on a program that has a function whose parameter is an vector of string. I want to use recursive on that function but everytime the function is called, I want to change the parameter to say for example
fun(stringArray[i])
where i is the number of time the function has been called.
So in simpler way something like following. But I need to keep track of how many times the function fun has been executed.
void fun(){
cout<<hi;
if(x!=10)
fun()
}
int main(){
fun();
}
In this one let's say I want to print it out just 10 times, so want to have a varible that increments, and when reaches 10, it stops. So in general what can I do to keep track of it? I tried using global variables but they don't seem to work with functions. Any suggestions?
use static variable inside the recursive function. static int i =0; and in the beginning of the function, say i++. every time the function is called, this i will be incremented.
The straightforward way is to do represent the recursive calls as tree. Each node of the tree is a call to the recursive function. The leaves of the tree are the calls to your recursive function which do not calls the recursive function.
To count how many times a function has been called, declare a count variable outside of the function, setting it to 0 . Inside of the body of the function reassign the variable incrementing it by 1 . The count variable will store the number of function invocations.
Example of a recursive function This recursive call can be explained in the following steps. Our recursion ends when the number reduces to 1. This is called the base condition. Every recursive function must have a base condition that stops the recursion or else the function calls itself infinitely.
I've seen quite a mess here so I decided to clear the things out.
Solution 0: Static Variable
Consider the code proposed with a small modification
#include<iostream>
using namespace std;
void fun()
{
static int count=1;
count++;
cout << "fun() is called " << count << " times" << endl;
if(count<=10)
{
fun();
}
}
int main()
{
cout << "first call" << endl;
fun();
cout << "second call" << endl;
fun();
cout << "third call" << endl;
fun();
}
resulting in this output:
first call
fun() is called 2 times
fun() is called 3 times
fun() is called 4 times
fun() is called 5 times
fun() is called 6 times
fun() is called 7 times
fun() is called 8 times
fun() is called 9 times
fun() is called 10 times
fun() is called 11 times
second call
fun() is called 12 times
third call
fun() is called 13 times
As you can see, using static variables could lead to some unexpected behaviour.
This is a one shot function that will cause you quite some headaches in the future. Furthermore, the usage of static variables leads to an unreadable code that is error prone
Just don't do it!
Solution 1: Variable passed by value
Consider this code:
#include <iostream>
using namespace std;
void fun(int i){
cout<<i<<endl;
if(i!=3) {
i++;
fun(i);
fun(i);
}
}
int main(){
fun(0);
}
This is the output:
0
1
2
3
3
2
3
3
1
2
3
3
2
3
3
As you can see the output is not the number of times the function is called
Solution 2: Variable passed by reference
#include <iostream>
using namespace std;
void fun(int& x){
if(x>=10)
return;
++x;
cout << x << endl;
fun(x);
}
void funEntry(){
int x = 0;
cout << "Entry point" << endl;
fun(x);
}
int main(){
funEntry();
funEntry();
}
will print
Entry point
1
2
3
4
5
6
7
8
9
10
This approach will work also with some more exotic recursive pattern like this one
#include <iostream>
using namespace std;
void fun(int i, int& x){
if(i>=4)
return;
++x;
cout << i << " " << x << endl;
fun(i+1,x);
fun(i+2,x);
}
void funEntry(){
int x = 0;
cout << "Entry point" << endl;
fun(0,x);
}
int main(){
funEntry();
funEntry();
}
Output:
Entry point
0 1
1 2
2 3
3 4
3 5
2 6
3 7
Entry point
0 1
1 2
2 3
3 4
3 5
2 6
3 7
Add a static
variable as counter.
#include<iostream>
using namespace std;
void fun()
{
static int count=1;
count++;
cout << "fun() is called " << count << " times" << endl;
if(count<=10)
{
fun();
}
}
int main()
{
fun();
}
static
variables are initialized only once and the value will be retained across function calls. See this link http://en.wikipedia.org/wiki/Static_variable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With