static functions are functions that are only visible to other functions in the same file (more precisely the same translation unit).
EDIT: For those who thought that the author of the questions meant a 'class method': As the question is tagged C he means a plain old C function. For (C++/Java/...) class methods, static means that this method can be called on the class itself, no instance of that class necessary.
static functions are functions that are only visible to other functions in the same file (more precisely the same translation unit).
EDIT: For those who thought that the author of the questions meant a 'class method': As the question is tagged C he means a plain old C function. For (C++/Java/...) class methods, static means that this method can be called on the class itself, no instance of that class necessary.
There is a big difference between static functions in C and static member functions in C++. In C, a static function is not visible outside of its translation unit, which is the object file it is compiled into. In other words, making a function static limits its scope. You can think of a static function as being "private" to its *.c file (although that is not strictly correct).
In C++, "static" can also apply to member functions and data members of classes. A static data member is also called a "class variable", while a non-static data member is an "instance variable". This is Smalltalk terminology. This means that there is only one copy of a static data member shared by all objects of a class, while each object has its own copy of a non-static data member. So a static data member is essentially a global variable, that is a member of a class.
Non-static member functions can access all data members of the class: static and non-static. Static member functions can only operate on the static data members.
One way to think about this is that in C++ static data members and static member functions do not belong to any object, but to the entire class.
When do you use static functions?
Reasons to use Static functions and variables in C - Stack Overflow
programming languages - False friends? Keyword "static" in C compared to C++, C# and Java - Software Engineering Stack Exchange
c - Static vs. non-static? (with non-OOP functions) - Software Engineering Stack Exchange
Can a static function return a pointer to a static variable?
Can a static function access static functions from other files?
Can a static function be recursive?
Videos
So far, I've been adding all function declarations to a functions.h file, which is included in all the source files. As this header has become enormous, I decided to change the design in the following way: whenever a function is used only in its translation unit, I declare it as static at the top of that translation unit, and not pollute my functions.h file.
Is this a good design? Is this how you use static functions?
When you talk about informing other readers, consider the compiler itself as a reader. If a variable is declared static, that can affect the degree to which optimizations kick in.
Redefining a static variable as extern is impossible, but the compiler will (as usual) give you enough rope to hang yourself.
If I write static int foo; in one file and int foo; in another, they are considered different variables, despite having the same name and type - the compiler will not complain but you will probably get very confused later trying to read and/or debug the code. (If I write extern int foo; in the second case, that will fail to link unless I declare a non-static int foo; somewhere else.)
Global variables rarely appear in header files, but when they do they should be declared extern. If not, depending on your compiler, you risk that every source file which includes that header will declare its own copy of the variable: at best this will cause a link failure (multiply-defined symbol) and at worst several confusing cases of overshadowing.
When you declare a static function the call to the function is a "near call" and in theory it performs better than a "far call". You can google for more information. This is what I found with a simple google search.
I have a book about the design of C++ by Bjarne Stroustrup (the inventor of C++). I don't have it right here so I can't lookup the exact quote right now, but in it he admits that when he added static to C++, he didn't fully understand what it meant in C. So that's why in C++ it has a different meaning than in C.
Java and C# inherited the meaning from C++.
Notably, C++ has both uses of static, and I think there's a third somewhere.
Generally, I think that the C use of static does not correspond at all to any English usage of the word, whereas I think that static as a static member variable, for example, makes a lot more sense.
Remember that as a language designer, you have a fair incentive to introduce fewer keywords into the language, to disallow less code- especially when you're trying to be source-compatible with C, as in C++, and the static keyword already existed, so they couldn't break any C programs trying to compile as C++ by re-using it.
Edit: I knew there was a third. C has function-level static variables. The static member is just a scoped version of this functionality. Therefore, both uses of static originate from C.
"Static" is overloaded to mean two things in C:
At the file level, it restricts scope to the current file. (I think this is what you're seeing.)
Within a function, it transforms variables such that their values are preserved between calls.
"Static" has little to do with performance in C. In the context in which you saw it used, it relates to scope-of-access. In this usage, "static" actually ends up being similar in function to access modifiers in an OOP language than with the "static" keyword as it applies to C++ methods.
Static, when applied to a function, means that the function has local scope or file scope, which will disallow someone from declaring a prototype to that function in a header file and using it somewhere else than where it was declared.
This has no performance implications, but is a good practice nevertheless. Why? Because it reduces scope. When the programmer spots a static function, it is an indication that the function will not be used in some other unknown place, thus, if you break that function, at least you know the damage should be confined to the source file in question.
Note though that static is not a hard guarantee that the function can't be called outside the file where is was declared, since someone can still grab a pointer to the function and externalize that pointer.