How does this function work?

This is a discussion on How does this function work? within the C Programming forums, part of the General Programming Boards category; Written and tested in Visual Studio Express Code: #include<stdio.h> void numberline(int depth); void main() { numberline(5); } void numberline(int depth) ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    17

    How does this function work?

    Written and tested in Visual Studio Express
    Code:
    #include<stdio.h>
    void numberline(int depth);
    
    void main()
    {
    	numberline(5);
    }
    
    void numberline(int depth)
    {
    if(depth == 0) {
    printf("0");
    return;
    }
    printf("%d", depth);
    numberline(depth - 1);
    printf("%d", depth);
    }
    Output: 54321012345

    I have debugged this little program: until the zero everything is obvious but then...
    What exactly is being returned (it is a void function)?
    Why that jump after the return to the last line of that numberline-function?
    Why is the variable being increased until it has the same number as the argument which you use for the function? Or is it not being increased and does the program read something from the memory?

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    87
    This page may explain it:
    Recursion in C and C++ - Cprogramming.com

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    One more thing: void main() is wrong. Use int main( void ) instead.
    Code:
    while(!asleep) {
       sheep++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How does this function work?
    By TheWhiffet in forum C Programming
    Replies: 3
    Last Post: 05-10-2011, 07:55 AM
  2. How to work on this function
    By void12 in forum C Programming
    Replies: 1
    Last Post: 02-07-2011, 11:16 AM
  3. oh, no! my function won't work.
    By student0806 in forum C Programming
    Replies: 5
    Last Post: 09-27-2010, 06:15 PM
  4. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM
  5. Trying to work with Cin Function
    By ProgrammingDlux in forum C++ Programming
    Replies: 14
    Last Post: 01-24-2002, 02:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21