What’s a keyword function for static
?
static
has different effects depending on your use and context. It can be found in: definition of functions, global and local variables, data members and member functions. How the question deals with the use of static
in place, the scope of the response remains the same.
Note: I’m only taking into account c++ to elaborate the answer. Some of the key concepts remain the same in c (as the link of the names), but I advise you to look for the differences.
a function static
how it works?
To understand this, we need to know what it means linkage (linkage in English) in c++.
A name which refers to an object, reference, function, type, template, namespace, value, or any other name found in [dcl.dcl]/declaration, may have linkage (being linkage the term in English).
If a name has a link, then it refers to the same entity that the name introduces in a definition in some other scope.
For example, in any function definition, as follows:
void foo() {
// ...
}
The identifier foo
is the name, and the whole function is the entity to which the name foo
refers.
If an entity (function, variable, etc.) is defined in multiple scopes with the same name, but does not have a sufficient connection between these names, then multiple instances of the same entity are generated. That is, which entity is referenced by name will depend on the scope in which the name was used.
There are some types of connection, being them:
- No connection (in linkage)
- Internal link (Internal linkage)
- External link (External linkage)
According to [basic.link]/2:
- When a name has external link, the entity denoted by the name can be accessed on other Translation Units (UT) (read informally as other files .cpp). Functions defined in a UT and used in other Uts are an example of such a name (function name) and entity (function itself).
- When a name has internal link, the entity denoted by the name cannot be accessed by other Uts, but can be accessed by other scopes within the UT in which the name was declared. Remember this one in particular, because this case is involved with the effect of
static
in a function statement.
- When a name has no connection, or no connection, the entity denoted by the name cannot be accessed nor outside the scope where it is declared. Local variables are an example, where scopes more funds can access it, but outside scopes do not.
If you keep reading until [basic.link]/3.1, you will find the following:
- To name having namespace Scope has Internal linkage if it is the name of
3.1 a variable, Function or Function template that is explicitly declared static
; or, [...]
Emphases of mine.
Translating freely, we have that any name with a namespace scope will have internal link if the name of a variable, function or function template is explicitly declared with static
.
Finally, answering the question, a function declared with static
makes your name have an internal connection. That is, the function can only be accessed by its name within the UT in which it is declared (that is, informally, only within the file .cpp where she resides). Some effects of this are:
- Ensures that the function can only be used within the UT where it resides;
- Consequently, your name will not conflict with scoped names outside UT;
- As this has to do with connection, Linker (the final program that calculates the binary symbols and links them) will have less work, as the function name is not exported;
- Gives more optimization possibilities for compilers, since the function cannot be used outside the UT, it can be fully optimized.
We can see an example of the last two items here.
when to use a function static
?
One of its uses is to maintain the encapsulation of a functionality that a UT offers, where the details of how the functionality is implemented do not interest the user of the interface. Only the functions exposed for use would have external link, while the details of the implementation remain with internal link.
static
has many meanings depending on where it is used, but in a global function such as this example acts as if it were invisible to Linker, and only local to the file.– Isac
Related: Static functions in C
– Guilherme Nascimento