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 8486579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:59:03+00:00 2026-06-10T20:59:03+00:00

Possible Duplicate: What is recursion really and explain the output of this program? What

  • 0

Possible Duplicate:
What is recursion really and explain the output of this program?
What is really happening in this code?

I have a recursive function which I don’t understand. I don’t even know if the code is valid or not:

int count(int x) // suppose x is 3
{
  if(x>0)
  {
    count(--x);
    printf(x);
    count(--x);  // control reaches here or not?
  }
}

This is just pseudocode. Take the initial variable as 3. Please explain with context the concept of stacks. This code is confusing me for days, and I can’t really find the answer for this code.

  • 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-06-10T20:59:05+00:00Added an answer on June 10, 2026 at 8:59 pm

    Ok, just because I needed to wake my brain up a bit 🙂

    count(3) will enter the function and call count(2) (2:nd level).
    count(2) will enter the function and call count(1) (3:rd level).
    count(1) will enter the function and call count(0) (4:th level).
    count(0) will enter the function, but since x>0 is false, it won’t do anything and just return to 4:th level where x is still 0.

    4:th level will output 0, and call count(-1) (5:th level)

    count(-1) will enter the function, but since x>0 is false, it won’t do anything and just return to 4:th level where x is still -1.
    4:th level returns to 3:rd level where x is still 1.

    3:rd level will output 1 and call count(0) (4:th level)

    count(0) will enter the function, but since x>0 is false, it won’t do anything and just return to 3:rd level where xis still 0.
    3:rd level returns to 2:nd level where x is still 2.

    2:nd level will output 2 and call count(1) (3:rd level)

    count(1) will enter the function and call count(0) (4:th level).
    count(0) will enter the function, but since x>0 is false, it won’t do anything and just return to 3:rd level where x is still 0.

    3:rd level will output 0 and call count(-1) (4:th level)

    count(-1) will enter the function, but since x>0 is false, it won’t do anything and just return to 3:rd level where xis still -1.

    3:rd level returns to 2:nd level where x is still 1.
    2:nd level returns to 1:st level and we’re done.

    Output is 0 1 2 0.

    I suggest that if you really want to understand this, try it yourself with count(4).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What is really happening in this code? in this problem i cant
Possible Duplicate: recursive array_diff()? I have a static multidimensional array which is always going
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: What is a RECURSIVE Function in PHP? What is a recursive function
Possible Duplicate: Solution to a recursive problem (code kata) give an algorithm to find
Possible Duplicate: Can every recursion be converted into iteration? Can always or which cases
Possible Duplicate: Can every recursion be converted into iteration? Are there problems in which
Possible Duplicate: Can a recursive function be inline? I think that recursive function defined
Possible Duplicate: Write a recursive function that reverses the input Recently, I've been reading
Possible Duplicate: else or return? Consider a typical recursive function: public int Fact(n) {

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.