2

Possible Duplicate:
What is a “static” function?

I have seen a function in a global namespace that is declared like this:

static int function_name(int a, double* p, int c, float * u)
{
     //do something with these arguments
}

What the static keyword means here?

EDIT: Now when I know what is for static, please explain what advantage gives the restriction of a function to be visible in a file only where it is declared? I mean why I should restrict my function visibility, what it gives to me?

Community
  • 1
  • 1
Narek
  • 38,779
  • 79
  • 233
  • 389

4 Answers4

6

The return value is not static int. The function is a static function returning an int.

See what is a static funciton

Community
  • 1
  • 1
Himadri Choudhury
  • 10,217
  • 6
  • 39
  • 47
  • But it is not a class member function, then what is it for the "static"? – Narek Mar 14 '11 at 15:24
  • In that case it means that the function has internal linkage, which means that it cannot be referred to from other translational units. Both uses are mentioned in the link that I gave. – Himadri Choudhury Mar 14 '11 at 15:26
  • 2
    What @DasBoot said is correct, but I'd like to explain a bit. "Translational unit" normally means: the same file. So if a function is declared static you can only call it within the same *.c file, not from other *.c files. The point is to avoid name collisions and also to make the function "private". – DarkDust Mar 14 '11 at 15:31
5

You would use a static function with the scope in the compilation unit when you really want the function with that name to be known only inside that compilation unit. A class or function that is not in that scope cannot accidentally call the function. (I'd have put this in a comment but I don't have privileges for that yet)

titania424
  • 457
  • 4
  • 13
  • One example I can think of is if you need to pass a callback function to some other API. – Dre Mar 14 '11 at 16:00
  • It's hard to come up with a specific example, because ideally, in C++, if you really wanted a utility function that was only seen in a certain scope, you'd put it in a class. I can see why you are wondering "why would I use this". I think it is more that it can be done, and it is because C++ is based on C, vs. some purity of the language and the ability to do this having some great meaning. Java changed that and said "put your functions in a class" so you can't do this. But in C++ it is there because of C. – titania424 Mar 14 '11 at 16:01
3

It isn't the return value that's static, it's the function. It means that the function is visible within that compilation unit (= file, approximately) but not elsewhere.

Gareth McCaughan
  • 19,888
  • 1
  • 41
  • 62
  • to be completely correct, a compilation unit != file, though that is most often the scope and is, in fact, the smallest scope. A case where a compilation unit is more than a file is when one file #include's another file. – Chris Cleeland Mar 14 '11 at 15:26
  • It's because "compilation unit" isn't strictly the same as "file" that I said "compilation unit" rather than just "file". It's because most of the time you can treat them as the same that I said "(= file)" rather than just "compilation unit". But yes, I should be a little more explicit about the fact that the two aren't quite equivalent; I'll add a note to my answer. (Apologies for the fact that it will make nonsense of your comment to anyone who doesn't bother reading this!) – Gareth McCaughan Mar 14 '11 at 15:36
1

As already mentioned, static refers to the whole function, not its return type. I want to add that the use of the static keyword in this context is deprecated in C++. Anonymous namespaces are the better way to go.

namespace 
{
   int function_name(int a, double* p, int c, float * u)
}
Community
  • 1
  • 1
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434