views:

983

answers:

13

I am thinking it is a best practice to declare them as static, as it makes them invisible outside of the module.

What are your thoughts on this?

A: 

If by 'module' you just mean a CPP file, you could just place the declaration and the definition right in the CPP file.

John Dibling
That doesn't really prevent the external symbol from being generated, so it could still conflict with one from another "module"
Evan Teran
+6  A: 

If it is truly an function which is internal only to that .c file, then yes. It should help avoid polluting the global namespace. Also, I think that the compiler is able to do some optimizations with calling conventions if the function is static since it knowns no other source file needs to know how to call it. This only really applies to c because as others have noted, c++ has namespaces to address this issue.

Evan Teran
+21  A: 

For C++, a better than static is to put it in an unnamed (anonymous) namespace. This is the preferred way to prevent pollution of the Global namespace.

namespace {
void myLocalFunction() {
// stuff
}
}
KeithB
another advantage about anonymous namespaces is that you can still pass the address of the objects inside it to templates, as they have still extern linkage.
Johannes Schaub - litb
+1, more info found in this SO thread: http://stackoverflow.com/questions/154469/unnamedanonymous-namespaces-vs-static-functions#154482
luke
disadvantage that it's not useful in C. The question was about c/c++
Ilya
I didn't notice the C tag. This is a C++ only solution.
KeithB
Didn't know 'static' was deprecated...
kshahar
Not exactly deprecated, it will continue to work. But namespaces are a better way that is recommended for any new code.
KeithB
One problem with anonymous namespaces : some debuggers have trouble with setting breakpoints by function name when you use them.
Arkadiy
+1  A: 

I think C and C++ have different constraints concerning static: in C you don't have namespaces and .c files are your modules, so it is really really important to put all non-public functions as static to prevent errors!

Piotr Lesnicki
A: 

In C++, you should use an anonymous namespace, like so:

// foo.cpp
namespace
{
   class Core { ... };
   void InternalFandango(Core *);
}

void SomeGloballyVisibleFunction()
{
   InternalFandango(&core);
}

Advantage: this is applicable to struct / class declarations, too.
In C, just mark the functions "static". There's nothing against using "static" in C++, too, but I've learnt to prefer the namespace, as it is one signle concept that works for all declarations.


As a side note: functions are "extern" by default (i.e. visible outside the compilation unit), variables are "static" (i.e. visible only inside the compilation unit).

peterchen
that's wrong. variables are extern. constants are static.
Johannes Schaub - litb
Yes - this is wrong - you should edit your answer to correct this - or you could get voted down!
Richard Corden
A: 

About the only potentially useful property I can think of for this use of "static" in C++, that anonymous namespaces don't provide, is that there's a warning in GCC you can switch on for unused static functions (a form of dead code). You don't get that for unused functions in anonymous namespaces, so in the unlikely event that you want the compiler to tell you when you stop using the function, do it that way.

Steve Jessop
A: 

A very similar question was asked before here. There is an edge case example where declaring the function static results in behaviour quite different to that of an unnamed namespace function. See my answer.

Richard Corden
+2  A: 

There was a lot about implementation details and not too much about concept.

Limiting the scope of variable/function etc.. is a good practice indeed. This is a basic concept of object oriented design - you want keep private as private. This way your interface is cleaner and code maintenance is easier. And you will not find one day that changing something that you considered as private broke compilation because somebody in another part of project liked your function and decided to use it.

Ilya
+1  A: 

In C, I make everything - functions and variables - static at file scope until I can demonstrate they're necessary outside the file. I'll make things static within a function if only that function will use them and they are not too huge. Basically, if the declaration is bigger than the rest of the function, I may put the declaration outside the function. And, of course, there's a header for the public services provided by a source file.

Jonathan Leffler
one should be careful using static in functions, in regards of reentrancy and multithreading.
Ilya
@Ilya: agreed. I try to make things const too whenever possible. It so happens that most of my work does not involve threading. However, I try to avoid global variables; I also try to avoid static variables, both at file and function scope, where I can.
Jonathan Leffler
A: 

In C code, make your functions static by default. Only make non-static functions and .h declarations for functions that will be needed by other modules.

In C++ code, put those functions that are local to the file into an anonymous namespace and make them static. In the GNU compiler at least, this will result in the best and smallest code, because no function will be written if all uses are inlined. If you intend it to be inlined, then of course marking it inline is even better than static.

I do not know why g++ writes the uncalled function bodies that are in anonymous namespaces into the output at all, but it does. Functions with hidden visibility seem to show up as well; marked as hidden symbols, but still producing unused code blocks in the object file. GCC probably doesn't understand that the code isn't needed in those cases. Or I am missing something, always possible.

Zan Lynx
A: 

If you're using GCC, then you should take a look at the visibility flag (see http://gcc.gnu.org/wiki/Visibility for a full discussion).

It'll hide symbols completely as opposed to marking them unreachable. That reduces the symbol table, and helps decrease link times.

Not only that, it opens the door for more inlining, if that's what you're after.

A: 

In c++, you'd declare the function private like this: (see italic)

class MyClass {
public:
void publiclyAccessibleFunction();
private:
void onlyAccesibleFromWithinTheClass();
int some_member_parameter;
};

A: 

Agreed. As a consequence, the prototypes for the static functions must go at the top of the .c file, not in the .h file.

Steve Melnikoff