By declaring it static, the variable sum persists across different invocations of the getSum function. It accomplishes the same thing as declaring sum as a global variable:
int sum = 0; int getSum( int n ) { if ( n > 0 ) { sum = sum + n; getSum( n - 1 ); } return sum; }
but limiting sum's visibility to the body of the getSum function.
This is generally not how you want to write a function like this, however. There's no need to create a persistent variable; you can just write it as
int getSum( int n ) { if ( n > 0 ) return n + getsum( n - 1 ); return n; }
although you'll want some sanity checking to make sure you don't overflow, and you probably want n to be unsigned int, since you only sum positive values, etc.
Thus,
getSum( 0 ) == 0 getSum( 1 ) == 1 + getSum( 0 ) == 1 + 0 == 1 getSum( 2 ) == 2 + getSum( 1 ) == 2 + 1 + getSum( 0 ) == 2 + 1 + 0 == 3
static intsumacts like a global variable that is only visible inside thegetSumfunction body. Does this answer your question?