Effect of return type being static

We use static data type when returning a pointer to a variable created in called function.for e.g

float * calculate_area(float r) 
{
    float *p;
    static float area;   
    p=&area;   
    area=3.14*r*r;
    return p;
}

If you would make area as automatic variable i.e without any type qualifier it would be destroyed when control returns from called function.When declared as static you could correctly retrieved the value of area from main also.Thus it in order for it to persists its value we make it as static.


The function is static, not the return type. This means that its name is only visible from within the current compilation unit, which is used as an encapsulation mechanism.

The function can still be called from elsewhere through a function pointer, however.

See also this discussion on the general static keyword for more context.

Tags:

C