Incorrect output from recursive function to compute sum of digits of a number?

Let's look at this recursive function in more detail: int sum (int a) { int f; if (a == 0) return f; f = (a% 10) + sum (a/10); } While you're on the right track and you have the right idea in general, your actual implementation is a bit buggy. For starters, let's look at these lines: if (a == 0) return f You have the right idea to terminate the recursion when a reaches zero, but the way you're doing it is a bit off. In particular, you're returning the value of the integer f but you've never initialized it.

This means that the return value is completely arbitrary. Instead of writing this, I think that you probably meant to write something closer to if (a == 0) return 0 which correctly says "if the number is zero, the sum of its digits is zero Similarly, take a look at the last line of your function: f = (a% 10) + sum (a/10) Again, your intuition is spot-on: the sum of the digits of a number are given by the sum of its first digit and the sum of the rest of its digits. However, notice that while you're correctly computing the sum of the digits, you aren't correctly returning the sum of the digits.In fact, you don't return anything at all if you execute this code, so the return value from the function is unspecified, hence the garbage output.

To fix this, consider rewriting the code like this: return (a % 10) + sum (a / 10) This actually says to hand back the value that you just generated right here, instead of storing it in a local variable that will be immediately cleaned up as soon as the function returns I believe that the reason you coded this function this way is that you're under the impression that the value of int f is carried across the function calls. Unfortunately, it is not. When writing a recursive function, each instance of the function is completely independent of each other instance and local variables accessible in one recursive call are not accessible in other recursive calls.

Consequently, even though each recursive call has its own variable int f those variables are all completely independent of one another. The value isn't carried through them. If you want to communicate values across recursive functions, the best way to do it is by using the return value of the recursive calls, or (if you must) by passing a pointer to some value down through the recursion Hope this helps!

Let's look at this recursive function in more detail: int sum (int a) { int f; if (a == 0) return f; f = (a% 10) + sum (a/10); } While you're on the right track and you have the right idea in general, your actual implementation is a bit buggy. For starters, let's look at these lines: if (a == 0) return f; You have the right idea to terminate the recursion when a reaches zero, but the way you're doing it is a bit off. In particular, you're returning the value of the integer f, but you've never initialized it.

This means that the return value is completely arbitrary. Instead of writing this, I think that you probably meant to write something closer to if (a == 0) return 0; which correctly says "if the number is zero, the sum of its digits is zero. " Similarly, take a look at the last line of your function: f = (a% 10) + sum (a/10); Again, your intuition is spot-on: the sum of the digits of a number are given by the sum of its first digit and the sum of the rest of its digits.

However, notice that while you're correctly computing the sum of the digits, you aren't correctly returning the sum of the digits. In fact, you don't return anything at all if you execute this code, so the return value from the function is unspecified, hence the garbage output. To fix this, consider rewriting the code like this: return (a % 10) + sum (a / 10); This actually says to hand back the value that you just generated right here, instead of storing it in a local variable that will be immediately cleaned up as soon as the function returns.

I believe that the reason you coded this function this way is that you're under the impression that the value of int f; is carried across the function calls. Unfortunately, it is not. When writing a recursive function, each instance of the function is completely independent of each other instance and local variables accessible in one recursive call are not accessible in other recursive calls.

Consequently, even though each recursive call has its own variable int f, those variables are all completely independent of one another. The value isn't carried through them. If you want to communicate values across recursive functions, the best way to do it is by using the return value of the recursive calls, or (if you must) by passing a pointer to some value down through the recursion.

Hope this helps!

Ya it helps a lot and you got the right point that I thought f is being traveled across iterations......Thanks a lot – Udit Gupta Aug 12 at 19:32 1 @Udit Gupta- Glad to help out! If you think this answers your question, you should mark the answer accepted so that the question is resolved. – templatetypedef Aug 12 at 19:42.

When a is 0, you are returning an uninitialized value (f was not initialized). Change it to: if (a == 0) return 0; You also forgot the return in the end of the function: return (a% 10) + sum (a/10); It is highly recommended that you always compile with the flag -Wall, which would warn you about those mistakes.

3 Not to mention the fact that there's no return statement at the end of sum. – Chris Aug 12 at 19:18 @Chris: Good point, I stopped looking for errors after finding the first, I'll update the answer. – fbafelipe Aug 12 at 19:20.

A%10+sum(a/10):0; – R.. Aug 12 at 20:54 codegolf.stackexchange. Com – yi_H Aug 12 at 21:09.

Your recursive function will calculate nothing it either returns an uninitialized int or nothing. You need to be returning the work you are doing in the function. Int sum (int a) { if (a == 0) { return 0; } return (a% 10) + sum(a/10); }.

You only return f is it is 0, but not if it isn't, which makes your return value undefined. I assume you want to do: int sum (int a) { int f; if (a == 0) return 0; f = (a % 10) + sum (a / 10); return f; }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions