return not returning expected number

This is a discussion on return not returning expected number within the C++ Programming forums, part of the General Programming Boards category; I have a recursive function that returns an integer. On one compiler it returns the correct number; on another compiler ...

  1. #1
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61

    return not returning expected number

    I have a recursive function that returns an integer. On one compiler it returns the correct number; on another compiler it returns the wrong number. My function as the following setup:

    Code:
    int recurse_function(node* location)
    {
            if(true)
            {
                 ++num;
                 recurse_function(newlocation);
             }
            else if(true)
            {
                  recurse_function(newlocation);
            }
            else
            {
                  cout<<"the num before return: "<<num<<endl;
                  return num;
            }
    }
    On both compilers the cout statement right before the return shows the correct number; yet on the second compiler -- and only on the second compiler -- it returns the wrong number. Any idea why? The second compiler is dev-C++...
    Last edited by lord; 10-10-2008 at 10:09 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Of course, the actual code doesn't (can't) look anything like that. But the idea is: return num only returns the number to the calling function -- which isn't your main function, but merely the most recent recurse_function. You need
    Code:
    return recurse_function(newlocation);
    Note that's also why your compiler is screaming "End of non-void function reached" or "Not all control paths return a value" or similar.

  3. #3
    Registered User lord's Avatar
    Join Date
    Dec 2006
    Posts
    61
    Thanks for that. It fixed the problem. It is interesting how the first compiler seemed to figure what I wanted -- though you were correct it was screaming at me.

    I didn't realize it returned to the most recent recurse_function...

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,528
    Quote Originally Posted by lord View Post
    Thanks for that. It fixed the problem. It is interesting how the first compiler seemed to figure what I wanted -- though you were correct it was screaming at me.
    You just got lucky. The compiler just happened to do what you thought the code did, despite you not actually coding it correctly.
    Quote Originally Posted by lord View Post
    I didn't realize it returned to the most recent recurse_function...
    If function A() calls function B(), you expect control to return to the next line of A() when B() returns. For example;
    Code:
    void A()
    {
        B();
        // control is here when B() returns
    }
    If B() happens to be A() - ie A() calling itself recursively - then why would you expect the control to go somewhere other than to the line after where A() was called?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 06:03 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  3. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  4. Alegro closes out on me
    By campsoup1988 in forum C++ Programming
    Replies: 8
    Last Post: 04-03-2006, 11:40 AM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09:39 AM

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