-2

what is the use of static variable in this program

/* program to find the addition of n numbers without using recursion:*/
#include<stdio.h>

int main(){

    int n,sum;

    printf("Enter the value of n: ");
    scanf("%d",&n);

    sum = getSum(n);

    printf("Sum of n numbers: %d",sum);

    return 0;
}

int getSum(n){

    static int sum=0;

    if(n>0){
         sum = sum + n;
         getSum(n-1);
    }

    return sum;
}
user2864740
  • 60,010
  • 15
  • 145
  • 220

3 Answers3

1

in this example sum will be declared only once.

which means that it will get its initial value (0) only when the function is called at the first place, every other time that the function will be called it will stay with its previous value just like a global variable.

there is another use of static and its pretty basic concept so you should read more about it there is a lot of information online.

As Fiddling Bits suggestes, do not confuse the sum from getSum and from main function, although you I said you can view sum as a global variable its scope is only in the getSum function and the instance in it is completely different from the one in the main. to summarize I am referring to the instance in getSum only.

antonpuz
  • 3,256
  • 4
  • 25
  • 48
1

First of all the function is wrong. Either its parameter has to be of type unsigned int or inside the function n has to be checked whether it is negative. Otherwise you can get an unexpected result when the argument will be a negative number.

As for the static variable then it is shared among calls of the function. It preserves its value between successive calls of the function. So it is used in the function as an accumulator.

I would write the function the following way

int getSum( n )
{
    if ( n == 0 ) return 0;
    else return n + ( n > 0 ? getSum( n - 1 ) : getSum( n + 1 ) );
} 

Take into account that it would be better if the function declaration is placed before function main. In this case the compiler can check whether the function is called correctly.

Also the comment before the program

/* program to find the addition of n numbers without using recursion:*/

is wrong because the function is recursive.:) It calls itself.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

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
John Bode
  • 119,563
  • 19
  • 122
  • 198