Pelles C forum

C language => Beginner questions => Topic started by: PhilG57 on December 28, 2013, 03:59:41 PM

Title: static functions
Post by: PhilG57 on December 28, 2013, 03:59:41 PM
Why would I ever code a function as "static" such as:

static int myfunction(int arg1, int arg2)
{
do some stuff here...
}


Do this specification must have to do with the visibility of the function within a single source file and/or across multiple source files in the same project?  Or, are the variables within the function affected in some way?

Seems to me the static specifier on a function means nothing.

Thanks.
Title: Re: static functions
Post by: Bitbeisser on December 29, 2013, 05:28:05 AM
Why would I ever code a function as "static" such as:

static int myfunction(int arg1, int arg2)
{
do some stuff here...
}


Do this specification must have to do with the visibility of the function within a single source file and/or across multiple source files in the same project?
Correct, a function declared as static is only visible with the same source file. That means you can not call such a function from a different C file of the same project, as the linker will not include any public information into the object file...
Quote
Seems to me the static specifier on a function means nothing.
See above...

Ralf
Title: Re: static functions
Post by: PhilG57 on December 29, 2013, 05:22:26 PM
Got it.  So the static specification on a function aids documentation by ensuring the function will not be called outside of the current compilation unit.  Many thanks.