I gave you an alternate route that doesn't require any static storage in an earlier post.
I gave you an alternate route that doesn't require any static storage in an earlier post.
Right. Try:
This is essentially the same code as you posted, except: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); }
- 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.
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.Originally Posted by coder1
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.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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
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.Originally Posted by coder1
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
edit: didn't read entire thread
"Simplicity is the ultimate sophistication." - Leonardo da Vinci
I still don't like the idea of that static object... you can only use the function (for its intended use) once :/
Umm ...
Code:unsigned int sumdigits(unsigned int n) { return !n ? 0 : n % 10 + sumdigits(n / 10); }
Yeah, that looks correct, though I note that you're not coder1 and probably didn't need the practice
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)