-2

While performing recursion to find the sum of first 25 natural numbers, to see which all numbers getting added I used printf to see but it shows 25 only once.

#include <stdio.h>
    
int sum_of_numbers(int);
int main(){
    int sum, num1 = 1;
    sum = sum_of_numbers(num1);
    printf("\nSum of first 25 natural numbers is %d.", sum);
}
int sum_of_numbers(int n1){
    int sum;
    if(n1 <= 25){
        printf("%d ", n1);
        sum = n1 + sum_of_numbers(++n1);
    }
    else
        return 0;
}

3 Answers3

1

Two problems.

In the event n is less than or equal to 25, you never explicitly return. This can cause weird things to happen.

Also, there's no reason to use ++n1 in this case when you can just use n1 + 1 since you're passing by value. The ++ operators can be useful, but the prefix ++ isn't doing what you probably think it is in this case.

Chris
  • 26,361
  • 5
  • 21
  • 42
1

Let's begin by looking at this function:

int sum_of_numbers(int n1){
    int sum;
    if(n1 <= 25){
        printf("%d ", n1);
        sum = n1 + sum_of_numbers(++n1);
    }
    else
        return 0;
}

For the moment, ignore the fact that this is a recursive function. In fact, let's replace the recursive call with one that isn't recursive:

int sum_of_numbers(int n1){
    int sum;
    if(n1 <= 25){
        printf("%d ", n1);
        sum = n1 + some_other_mystery_function(++n1);
    }
    else
        return 0;
}

Based on experience, when you're first learning recursion, it's often helpful to pretend the recursive calls are to different functions. Many errors in recursion are really simpler errors in function call and return that get overlooked because you're focusing too much on the recursive bit.

With that change made - do you notice anything unusual about this function? Specifically, what value gets returned if you pass in a value n1 that's less than or equal to 25? In that case, the function reaches the end without ever returning anything. Specifically, we enter the if statement, execute the printf, make the call to some_other_mystery_function, then fall off the end of the function with nothing returned. In C, that's undefined behavior, and the value that gets returned is essentially up to the whim of the compiler, the OS, and the alignment of the planets.

This leads to general transferable skill 1: make sure, when writing recursive functions, that you always return a value. That's true about most functions, but especially in the case of recursion.

There's also something else a bit suspicious here. Notice that you set the sum variable equal to some value, but sum is a local variable that's never referenced later in the function. That should trigger some alarm bells - why are we setting a local variable without reading it?

I think what you meant was something more like this:

int sum_of_numbers(int n1){
    if(n1 <= 25){
        printf("%d ", n1);
        return n1 + some_other_mystery_function(++n1);
    }
    else
        return 0;
}

This always returns a value, and never writes to a local variable that isn't read.

There's another issue here, though. Look at this line:

return n1 + some_other_mystery_function(++n1);

Here, you're reading from n1 at one point in the expression, and writing ++n1 in another. That's a Bad Thing, because there isn't a way to guarantee whether the first use of n1 evaluates to "what n1 used to be before the ++n1 was evaluated" or "the new value of n1 after we did ++n1." Then again, notice that this statement is a return statement, so after this line executes there's never going to be another chance to touch n1 again. I think you meant something like this?

return n1 + some_other_mystery_function(n1 + 1);

This more clearly means "pass n1 + 1 as the argument to the function."

That leads to general, transferable skill number 2: avoid using ++, +=, -=, etc. as parameters to functions. In some cases this will work correctly, but in many cases it introduces errors. Instead, pass value + 1, value + 137, value - 271, etc.

And finally, transferable skill number 3 - the compiler can warn you about all of these issues. Most compilers these days, when you crank the warning settings up, will warn you about writing to local variables that aren't read, or falling off the end of a function without returning anything, or incrementing a variable used elsewhere in an expression. I would recommend enabling those settings, since that would likely have tipped you off that something wasn't quite right here. You might not have understood what the warnings were all about, and that's fine! If that happens, feel free to post your code and the warning message in a separate question on the site, and other folks can take a look at it.

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
0

First of all, you are not returning the value from the body of the recursive function, though my GNU g++ compiler is not complaining about it and doing the work, but you cannot assume it to work.

Second, you are incrementing the variable ++n1 which is also increasing the value for n1 in the calling statement. So what you are actually doing is adding 2 to 26, not 1 to 25. You can use n1 + 1 instead, you don't need to change the variable and you should not. You can change your code to following to address the issues:

#include <stdio.h>
    
int sum_of_numbers(int);
int main() {
    int sum, num1 = 1;
    sum = sum_of_numbers(num1);
    printf("\nSum of first 25 natural numbers is %d.", sum);
}

int sum_of_numbers(int n1){
    int sum;
    if (n1 <= 25) {
        printf("%d ", n1);
        sum = n1 + sum_of_numbers(n1 + 1);
        return sum;
    }
    else {
        return 0;
    }
}
let me down slowly
  • 822
  • 10
  • 27