Thread: returning random value.

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I gave you an alternate route that doesn't require any static storage in an earlier post.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Right. Try:
    Code:
    #include <stdio.h>
    
    int sumdig(int num);
    
    int main(void)
    {
        int a;
        a = sumdig(12345);
        printf("%d\n", a);
        return 0;
    }
    
    int sumdig(int num)
    {
        static int sum;
        int a, b;
        a = num % 10;
        b = (num - a) / 10;
        sum = sum + a;
        if (b != 0)
            sumdig(b);
        return(sum);
    }
    This is essentially the same code as you posted, except:
    • I declared sumdig before calling it.
    • I formatted the code properly.
    • I defined sumdig with an explicit return type of int.
    • I made it such that sumdig always returns sum, instead of having a control path that does not return a value despite the function being declared as returning an int.

    Examining the logic, I'm reasonably certain that you will indeed get 15 as the output, since sum will be zero initialised. If you don't get this output, then the problem lies elsewhere, e.g., you are running a stale version of your program.

    Quote Originally Posted by coder1
    static should not cause a problem because without it the whole program will not work as sum will always be initialized each time with the value we give and will not work as expected by the program
    Well, it is true in this case because you are only calling sumdig once in main. As was mentioned to you earlier, once you call sumdig more than once, this use of a static local variable becomes problematic.

    whiteflags' solution is one option, but I don't recommend it here. Rather, use what Salem suggested in post #4, which is what I suggested in post #11.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    F#ck me Freddy!!
    Join Date
    Sep 2013
    Location
    jaipur
    Posts
    79
    wow,it works fine now .Thank you for coping up and working out the problem for me and not ..........ing about how idiotic i am and i am copying code like some others people here.
    I implemented your improvements step by step and found out that the problem was not returning sum each time.That was the problem .So i think if we don't return "sum" it will not get stored and a garbage value is stored in place of it and because of this i was getting random values as answer.
    But i have a question the return function will only works one time when "if" is false since for every true "if" recursion occurs like the "else" will do.so why we have to eliminate else as it will not matter in my opinion.Please clarify this

    And thanx again you made my day ! why you call yourself witch you are doing work of an angel

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by coder1
    But i have a question the return function will only works one time when "if" is false since for every true "if" recursion occurs like the "else" will do.so why we have to eliminate else as it will not matter in my opinion.
    Firstly, a return statement is not a function. Now, the thing is that the return happens on each and every call to sumdig. If b != 0, control goes into the recursive call to sumdig, but after control leaves that recursive call, it reaches the return statement in the current call to sumdig.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    edit: didn't read entire thread
    "Simplicity is the ultimate sophistication." - Leonardo da Vinci

  6. #21
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    I still don't like the idea of that static object... you can only use the function (for its intended use) once :/

  7. #22
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Umm ...
    Code:
    unsigned int sumdigits(unsigned int n)
    {
    	return !n ? 0 : n % 10 + sumdigits(n / 10);
    }

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yeah, that looks correct, though I note that you're not coder1 and probably didn't need the practice
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an integer ... not returning; weird error
    By Imanuel in forum C++ Programming
    Replies: 19
    Last Post: 09-25-2011, 01:30 PM
  2. Recursion: base case returning 1, function returning 0
    By yougene in forum C Programming
    Replies: 5
    Last Post: 09-07-2007, 05:38 PM
  3. Replies: 4
    Last Post: 11-16-2004, 07:29 AM
  4. Replies: 2
    Last Post: 12-25-2003, 01:31 AM
  5. Replies: 1
    Last Post: 12-14-2002, 01:51 AM