A static function is a function with the static qualifier applied:
static void foo ( void )
{
/* Blah blah */
}
What it does is restrict visibility of the function to the translation unit in which it's declared. Functions are implicitly declared as extern by default, which means they're visible across translation units. You can compile the following, but it won't link:
/* file1.c */
void foo ( void )
{
}
extern void bar ( void )
{
}
static void baz ( void )
{
}
/* file2.c */
void foo ( void );
void bar ( void );
void baz ( void );
int main ( void )
{
foo(); /* OK: foo is extern by default */
bar(); /* OK: bar is explicitly extern */
baz(); /* Wrong: baz isn't visible in this translation unit */
}
Static function are used to avoid the access of the function from the other module.
By default normal functions can access by any module from the file; to avoid we can use static key word to the function.
Also if you have more than one; same functions name across file and if they were in static we will not get any errors.
No comments:
Post a Comment