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.