What is a Static function for?

Asked

Viewed 4,494 times

13

What is a function for with the keyword static? I know that declaring a local variable as static within a function it will function as if it were a global variable, and a function static how does it work? And when you should use a function static?

I thought a function like static if you have inside a loop she would be called only once, but that’s not what happens in the example below:

static void MostrarMensagem()
{
    printf("Mostrando uma mensagem de uma funcao static!\n");
}

int main()
{
    while(1)
    {
        MostrarMensagem();
    }
    return 0;
}
  • 1

    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.

4 answers

11


Do you know languages classified as object oriented? Do you know private?

That’s it, when you put the static in the function is saying that it can only be accessed within its own source file, it is deprived of that code and cannot be called by other parts of the application. In C++ it is not recommended to use.

It does not have the same semantics as Java or C#. When used in class it even has similar but not identical behavior. The semantics is equal to C, the access restriction is in the file and not in the class. The only difference is that by being in the class the full name of the function includes the class name.

And static is not a function as described in the question, it is only a scope modifier.

  • What about a function that is not part of a class? A global function like the example.

  • @cYeR is exactly that, as you put it in the question.

  • Oh yes sorry, I read in a hurry, now it’s clear. Thank you

  • static is widely used in C++ to let internal statement linkage and not populate the final binary with unused symbols. So much so that exists namespace {} (namespace anonymous) for other types of statements/definitions, such as structs/classes. I’m afraid your statement "In C++ is not recommended to use" is wrong.

  • @Márioferoldi see the context of the question. And I don’t know what namespace has to do with this.

  • @Maniero The context of the question is clear, but my comment refers to his phrase "In C++ it is not recommended to use", and such a statement has no basis. About the namespace, anonymous namespaces give internal linkage to declared/defined names within a. It has the same effect as static, with the addition of being able to be applied to more names besides functions and global variables. I just cited as an example that such a strategy is used and has even more support, which denies its statement.

  • Well, you denied your statement in the paragraph itself. If you have a better mechanism, if the existing mechanism for C compatibility is not necessary, do you still think it is recommended to use? (the question is rhetorical)

  • Well, with MSVC, names have internal linking by default if they are not used outside of your translation unit. It is common to prefer internal linkage to things that will only be used within a UT, gives even more opportunities for compiler optimizer and maintains encapsulated functionality. You can keep a good part of the code with Internal linkage and expose the functionality through a few functions with Xternal linkage and maintain compatibility with C (I don’t know where the motivation of the issue came from, but here’s the counterargument in any case.)

  • I believe I was wrong about MSVC, this is a different case for names exported to DLL. But I remain in the same position with the rest of the comment.

Show 4 more comments

5

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 to elaborate the answer. Some of the key concepts remain the same in (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 .

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:

  1. 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.

0

Oops!! I saw several more answers, inconsistent with what the initial person asks. The great majority here spoke or referred a lot to the POO - Progress Oriented Objects, in this, everyone is right. but this is not what the beginner of the question wanted to know. It is as follows: in C (pure C), when we use a Static function, it is called by another function, thus: "main" calls a function1 and this function1 calls another function, function 2. This other function2, if declared as "Static", only function1 can call function2, it will be restricted to function 1. Example: 'Main" calls a function to make a sum, but this sum must be done with some exchanged values (permuted), then the sum function, before summing, will call the permutation function (Static) and only after returning the result of the "Static) function" is that it will perform the sum and deliver the result to the "main".

0

  • 4

    It appears that the author refers to global static functions and not class-associated

  • 1

    Isac said it all, I’m referring to a static global function not to classes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.