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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:48:19+00:00 2026-05-21T10:48:19+00:00

I am new to C and I’m reading about recursion, but I am totally

  • 0

I am new to C and I’m reading about recursion, but I am totally confused.

The main part where I’m getting confused is how things get unwind when the exit condition is reached. I would like to know how during recursion values got pushed and popped from stack.

Also can anyone please give me a diagramatic view of recursion?

Thanks…

  • 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-21T10:48:20+00:00Added an answer on May 21, 2026 at 10:48 am

    Lets assume a function:

    int MyFunc(int counter) {
        // check this functions counter value from the stack (most recent push)
    
        // if counter is 0, we've reached the terminating condition, return it
        if(counter == 0) {
            return counter;
        }
        else {
            // terminating condition not reached, push (counter-1) onto stack and recurse
            int valueToPrint = MyFunc(counter - 1);
    
            // print out the value returned by the recursive call 
            printf("%d", valueToPrint);
    
            // return the value that was supplied to use 
            // (usually done via a register I think)
            return counter;
        }
    }
    
    int main() {
        // Push 9 onto the stack, we don't care about the return value...
        MyFunc(9);
    }
    

    The output is: 012345678

    The first time through MyFunc, count is 9. It fails the terminating check (it is not 0), so the recursive call is invoked, with (counter -1), 8.

    This repeats, decrementing the value pushed onto the stack each time until counter == 0. At this point, the terminating clause fires and the function simply returns the value of counter (0), usually in a register.

    The next call up the stack, uses the returned value to print (0), then returns the value that was supplied into it when it was called (1). This repeats:

    The next call up the stack, uses the returned value to print (1), then returns the value that was supplied into it when it was called (2). etc, till you get to the top of the stack.

    So, if MyFunc was invoked with 3, you’d get the equivalent of (ignoring return addresses etc from the stack):

    Call MyFunc(3) Stack: [3]
    Call MyFunc(2) Stack: [2,3]
    Call MyFunc(1) Stack: [1,2,3]
    Call MyFunc(0) Stack: [0,1,2,3]
    Termination fires (top of stack == 0), return top of stack(0).
    // Flow returns to:
    MyFunc(1) Stack: [1,2,3]
    Print returned value (0)
    return current top of stack (1)
    
    // Flow returns to:
    MyFunc(2) Stack: [2,3]
    Print returned value (1)
    return current top of stack (2)
    
    // Flow returns to:
    MyFunc(3) Stack: [3]
    Print returned value (2)
    return current top of stack (3)
    
    // and you're done...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

New to PHP and MySQL, have heard amazing things about this website from Leo
New to Android so I'm writing small programs to get familiar with how things
New developer here,Im using the Custom Image Picker by ray wenderlich. But what I
New to spring. I'm getting the following exception being caught: 2012-06-14 16:20:57,719 [http-8080-6] ERROR
new AjaxOptions { OnBegin = MyFunction, Url=/Controller/JSONAction/+OnbeginRetunrValue, HttpMethod=GET } In my Ajax properties, I
New to MonoTouch and MonoTouch.Dialog, but I'm sure it's possible to create a tab
New question, but really close to need to refresh page in jquery mobile .
New to java. Unclear about this piece of code: ParseQuery query = new ParseQuery(GameScore);
new System.Threading.Thread(() => { var myObject = new CustomObject(); myObject.SomeEvent += SomeMethod; }).Start(); Part
New to WCF, but familiar with COM+ - can I wrap a WCF service

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.