int countdown(int num)
{
if (num ==1)
{
return 1;}
else
{
return num + countdown(num-1);}
}
I want to know the current value of the function each step.How can i print out the value of countdown function each time num decrease? Thanks
This can do it-
int countDown(int num){
cout<<num<<endl;
if(num==1)
return 1;
else{
int r= countDown(num-1);
cout<<"Countdown: "<<r<<endl;
return num + r;
}
}
each time it recursively calls the countDown function, the first cout will show you the value of num variable and second cout will show you the value of your recursive function after decrement each time.
If I understand correctly, you want the value returned by countDown(num-1) each time. You can try this.
int countdown(int num)
{
if (num ==1)
{
return 1;}
else
{
int temp = countDown(num-1);
count<< temp<<endl;
return num+temp;
}
Let me know if this worked for you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.