i-- changes the value of i after you have used it.
triangle_count(i--) gives you triangle_cout(i), and afterwards the value of i is changed.
triangle_count(i-1) gives you triangle_count(i-1), and i is unchanged.
Here, run this code:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
int main()
{
int i = 7;
std::cout << "i = " << i << std::endl;
std::cout << "i-1 = " << i-1 << std::endl;
std::cout << "i-- = " << i-- << std::endl; // Here, the output value is 7
std::cout << "i = " << i << std::endl; // And here i is 6
return 0;
}
i-- will give you the value i and then decrease it by 1. i-1 will give you the value i-1 and then the value of i will remain the same.
I think what you are looking for is --i which will decrease i by 1 and then return the new value of i.
That being said, there is a major flaw in your function. Whenever we come across the return keyword, we exit the function. Therefore your function is essentially equivalent to this:
1 2 3 4 5 6 7
int triangle_count(int i)
{
if(i>0)
return (i+triangle_count(i-1));
elsereturn 0;
}