I was doing some C# practice and decided to make a basic function to sum the contents of an integer array.
Originally I wrote my code as follows:
if(index == 0)
return toSum[index];
else
return toSum[index] + sum(toSum, index--);
Now that code resulted in a StackOverFlow exception. This made no sense to me; surely this is how one would do a summation? Turns out the problem was in the index--
. When I changed it to index - 1
it worked out fine, thus I was wondering why is that the case? My understanding is that it is simply a shorthand for index = index-1
. I was wondering if anyone could explain the reason behind this behavior.