Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6646691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:26:51+00:00 2026-05-26T00:26:51+00:00

This program recursively calls the function tester . #include <iostream> void tester(); int i

  • 0

This program recursively calls the function tester.

#include <iostream>
 void tester();
 int i = 1;
 int k = 1;

 int main() {
   tester();
 }

 void tester() {
  while(i++ < 10)
    tester();
  std::cout << "called " << k++ << " times" << std::endl;
 }

I am surprised by this output :

called 1 times
called 2 times
called 3 times
called 4 times
called 5 times
called 6 times
called 7 times
called 8 times
called 9 times
called 10 times

and it is because this is the way i understand this program :

After the first call to tester from main it enters a loop. The first statement of the loop calls the function tester again, and this carries on.After looping 10 times the statement following the while loop should work i.e only once . So the output should be :called 1 times . But this isn’t actually happening ! Why ? How does this program work ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:26:51+00:00Added an answer on May 26, 2026 at 12:26 am

    The while body is executed only once in each recursive depth. The while is false when i is 10, which will be when tester () is called 10 times recursively. As the i is declared global, the i++ update will be visible to each call of tester ().

    At the last recursive depth when the while condition is false, the last tester () call will return to its previous depth. At this moment, the next iteration of the while loop will be false as i is 10. The cout statement will be encountered after each while loop is terminated, which will sequentially print the values of k, incrementing at each recursive depth, as the recursion rolls back.

    Manually trace what happens to understand the stuff.

    UPDATE

    Have a look at the execution output. Especially note the d parameter, which denotes the recursive depth. At each depth the while loop has iterated one time, upto the last “while is true, tester called” output. At depth 10, the while is false as it is 10, and first time it returns back the control to its previous depth level, (the first “return back” when i is 10). After it returns the control returns to the body of the while loop of the last level, from which the function was called (from which it just returned), and the next iteration of this loop will be false, (i is global and 10 or more), therefore this also returns. Similarly, at each recursive depth the 2nd iteration of while loop is false, and it keeps returning. Check out the output.

    i   k  d
     1  1  0, call from main
     2  1  1, while is true, tester called
     3  1  2, while is true, tester called
     4  1  3, while is true, tester called
     5  1  4, while is true, tester called
     6  1  5, while is true, tester called
     7  1  6, while is true, tester called
     8  1  7, while is true, tester called
     9  1  8, while is true, tester called
    10  1  9, while is true, tester called
    11  1 10, returning back, print k    // this step the while is false in depth 10
    12  2  9, returning back, print k    // from now on, as the recursion rolls back
    13  3  8, returning back, print k    // the second iteration at each recursive 
    14  4  7, returning back, print k    // depth will be executed, and each while
    15  5  6, returning back, print k    // condition will be false, therefore it will
    16  6  5, returning back, print k    // not call tester anymore and return the control
    17  7  4, returning back, print k    // to the previous level. NOICE the `d' parameter
    18  8  3, returning back, print k
    19  9  2, returning back, print k
    20 10  1, returning back, print k
    

    And here is the test code. I hope the description is much clear now (?). Analyzing this will help.

    #include <stdio.h>
    
    int i = 1;
    int k = 1;
    
    void tester (int d)
    {
      while (i++ < 10)
      {
        printf ("%2d %2d %2d, while is true, tester called\n", i, k, d);
        tester (d+1);
      }
      printf ("%2d %2d %2d, returning back, print k\n", i, k++, d);
    }
    
    int main (void)
    {
      int depth = 0;
      printf ("i   k  d\n");
      printf ("%2d %2d %2d, call from main\n", i, k, depth);
      tester (depth + 1);
    
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For this program #include <iostream> using std::cout; struct C { C() { cout <<
Consider this program: #include <map> #include <vector> typedef std::vector<int> IntVector; typedef std::map<IntVector,double> Map; void
Consider this program int main() { float f = 11.22; double d = 44.55;
Is this program well-defined, and if not, why exactly? #include <iostream> #include <new> struct
There is a function that calls itself recursively infinitely. This function has some arguments
Here is a function in my program void quicksort (int *num, int p, int
Hello I have this program which reverses letters I enter. I'm using iostream .
This program I use has it's own variables to set when you run it,
This program stores pairs in a map, counting the number of times a word
This program reads emails (really just a .txt file structured like an email) and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.