What are the possible effect of returning a static type data. And when should we actually use it?
static ssize_t
my_read(int fd, char *ptr)
{
//code from Stevens Unix Network programming.
if (something)
return (-1)
if (something else)
return (0)
return (1)
}
why static here?
Thanks.
it is a static method (static), it does not return any result (return type is void), and. it has a parameter of type array of strings (see Unit 7).
static return type follows Liskov Substitution Principle. A child class method can return a narrower class object than the parent methods return type. Because static always refer to the class name of the called object (i.e. same as get_class($object) ), static is a subset of self , which in turn is subset of parent .
It is the variable that is static, not its value. In fact, however, it would also be fine to return a pointer to the variable ( &person_p ), which is as close as you can come to returning the variable itself.
In Java, a static method may use the keyword void as its return type, to indicate that it has no return value.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With