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?

3
  • It's not the return type which is static, it's rather the function which is declared static! Commented Mar 14, 2011 at 15:35
  • Please stop voting for closing this question. Let's get the answer of my edit part as well. Commented Mar 14, 2011 at 15:42
  • Fix the title, then. Or let it be closed and ask a new question about the benefits of file-static. Commented Mar 14, 2011 at 16:28

4 Answers 4

6

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

See what is a static funciton

Sign up to request clarification or add additional context in comments.

3 Comments

But it is not a class member function, then what is it for the "static"?
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.
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".
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)

2 Comments

One example I can think of is if you need to pass a callback function to some other API.
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.
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.

2 Comments

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.
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!)
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)
}

7 Comments

This specific deprecation has been un-deprecated in C++0x, by the way.
It is actually un-deprecated again in C++0x, so it is not obviously too bad to use it. :-)
But why to use it? What it gives me?
@Narek: This is a way to specify that your function has internal linkage, that is, it cannot be seen or called from other translation units (cpp files).
@Narek: Are you asking about a useful application of static in C++, or are you wondering why it exists? Hint: C does not have namespaces.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.