I was trying to write a function that would compute the sum of the digits of a number using recursion, but the output is incorrect. Here’s the code:
/*Write a function to calculate sum of digits of a number using recursion*/
/*Author:Udit Gupta Date:10/08/2011*/
#include<stdio.h>
int sum (int);
int main () {
int n,s;
printf ("Enter the number:");
scanf ("%d",&n);
s = sum (n);
printf ("The sum of the digits of the number is %d",s);
}
int sum (int a) {
int f;
if (a == 0) {
return f;
}
f = (a% 10) + sum (a/10);
}
Here are some of the output values:
udit@udit-Dabba ~/Desktop/letusc/ch5/J $ ./a2.out
Enter the number:123
The sum of the digits of the number is 7
udit@udit-Dabba ~/Desktop/letusc/ch5/J $ ./a2.out
Enter the number:1234
The sum of the digits of the number is 2919930
udit@udit-Dabba ~/Desktop/letusc/ch5/J $ ./a2.out
Enter the number:123456
The sum of the digits of the number is 4620297
udit@udit-Dabba ~/Desktop/letusc/ch5/J $ ./a2.out
Enter the number:12345
The sum of the digits of the number is 15 /*Only this one seems correct*/
Can someone help me figure out why this isn’t working correctly?
Let’s look at this recursive function in more detail:
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:
You have the right idea to terminate the recursion when
areaches zero, but the way you’re doing it is a bit off. In particular, you’re returning the value of the integerf, 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 towhich 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:
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:
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 variableint 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!