Thread: Need help understanding function

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    16

    Need help understanding function

    I am reviewing material for finals when i cam across this question to find out what prints from the following function

    when print(4)


    Code:
    void print(int i)
    {
         if(i>=1)
         {
                 printf("%d",i);
                 print(i-1);
                 printf("%d",i);
         }
    }

    I understand that it enters with 4, the function is called within itself for i-1 so it reenters with 3, prints 3, than does it until i is 1 enters for the last time which then i which is 1 and 1-1 = 0, so it cannot enter the function no more however when i put compile the function i get the following


    43211234, can anyone explain this output

    i understand the 4321, thereafter is where i am lost

    thanks in advance
    Last edited by peste19; 05-13-2012 at 09:31 AM.

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    16
    anyone?

  3. #3

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Patience child. Don't bump your thread, it's against the forum guidelines. Read this: Announcements - C Programming.

    Now, think about what happens after the call to print(i-1). Where do you return to when the recursive function is done? What happens after that point?

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    If you have GDB available you can step through the function and observe what happens, compile with -g flag then:

    Code:
    gdb a.out
    start
    step
    
    (hit enter and observe)
    The thing to keep in mind IMO is that functions are pushed to the stack.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    16
    Quote Originally Posted by anduril462 View Post
    Patience child. Don't bump your thread, it's against the forum guidelines. Read this: Announcements - C Programming.

    Now, think about what happens after the call to print(i-1). Where do you return to when the recursive function is done? What happens after that point?
    my bad, to be honest we didnt cover recursion in class, so that i guess why it doesnt make sense to me, anyways upon first look i thought that it enters at 4, print out the int then it would get to that function call and restart with 3 until it got to 0 and then wouldnt enter and it would always skip the second printf because of the function call
    Last edited by peste19; 05-13-2012 at 10:28 AM.

  7. #7
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    143
    Quote Originally Posted by peste19 View Post
    ... and it would always skip the second printf because of the function call
    What happens on any other function when it calls another function? For example:
    Code:
    #include <stdio.h>
    
    void afunction(void)
    {
        int i;
        printf("Enter an integer:");
        scanf("%d", &i);
    }
    Would the scanf() be skipped because we call printf()? Remember printf() and scanf() are just function calls.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help understanding this function...
    By Siaw Ys in forum C Programming
    Replies: 1
    Last Post: 12-02-2011, 02:03 PM
  2. Need help with understanding a recursion function
    By CS_Student8337 in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 06:52 AM
  3. Help understanding result of Function
    By deadhippo in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 02:56 AM
  4. need help understanding this function
    By Lince in forum C Programming
    Replies: 4
    Last Post: 08-04-2007, 10:29 AM
  5. Problems with understanding a function
    By majoub in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:00 AM