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.
static function in C - Stack Overflow
Have you ever used a static function?
Static Function Usage in C - Programming & Development - Spiceworks Community
When do 'static functions' come into use? - Software Engineering Stack Exchange
Can a static function return a pointer to a static variable?
Can a static function be recursive?
Can a static function access static functions from other files?
Videos
Making a function static hides it from other translation units, which helps provide encapsulation.
helper_file.c
int f1(int); /* prototype */
static int f2(int); /* prototype */
int f1(int foo) {
return f2(foo); /* ok, f2 is in the same translation unit */
/* (basically same .c file) as f1 */
}
int f2(int foo) {
return 42 + foo;
}
main.c:
int f1(int); /* prototype */
int f2(int); /* prototype */
int main(void) {
f1(10); /* ok, f1 is visible to the linker */
f2(12); /* nope, f2 is not visible to the linker */
return 0;
}
pmg is spot on about encapsulation; beyond hiding the function from other translation units (or rather, because of it), making functions static can also confer performance benefits in the presence of compiler optimizations.
Because a static function cannot be called from anywhere outside of the current translation unit (unless the code takes a pointer to its address), the compiler controls all the call points into it.
This means that it is free to use a non-standard ABI, inline it entirely, or perform any number of other optimizations that might not be possible for a function with external linkage.
I understand that they're used to limit the scope of a function to that object file, but can't wrap my head around why anyone would want to do that?
static int foo(void) {
...
}What's the purpose of these? The only thing I've found convincing is:
As usual, the smaller the scope, the better, so always declare functions static
if you can.In C programming, files are often used to represent "classes", and static
functions represent "private" methods of the class.A common C pattern is to pass a this
struct around as the first "method" argument, which is basically what C++ does under the hood.
The answer is from this SO post
Assuming that you're using OOP, use static functions when they don't depend on any class members. They can still be private, but this way they are optimized as they don't depend on any instance of the related object.
Other than the above, I find static functions useful when you don't want to create an instance of an object just to execute one public function on it. This is mainly the case for helper classes that contain public functions to do some repetitive and general work, but don't need to maintain any state between calls.
Trying for a more simplified explanation than the above (pretty good ones).
An object is code + data normally. A static method is used when you only have the "code" portion to deal with (there is no data/state being maintained (except static data members)).