Search code examples
cfunctionstaticstatic-functions

What is the advantage of a static function?


The declaration below:

static int *foo();

declares foo as a static function returning a pointer to an int.

What's the purpose of declaring a function as static ?


Solution

  • The function's name isn't visible outside the translation unit (source file) in which it's declared, and won't conflict with another function foo in another source file.

    In general, functions should probably be declared static unless you have a specific need to call it from another source file.

    (Note that it's only the name that's not visible. It can still be called from anywhere in the program via a pointer.)