What's the difference between these two non-member functions ?
static void
function1() {
std::cout << "Test" << std::endl;
}
void
function2() {
std::cout << "Test" << std::endl;
}
EDIT : I know static means that the function has internal linkage and by default a function in the global scope has external linkage but I don't really see how it will be different then. If we declare a function static or if we don't, we still have to include the file containing the function to use it in another file. It doesn't depend if the function is static or not, right ?
According to cppreference.com :
Internal linkage : The name can be referred to from all scopes in the current translation unit.
External linkage : The name can be referred to from the scopes in the other translation units.
When you define a non-member function (or any data type for that matter) as static, you are essentially saying that this symbol is defined in this file and will not be referenced in other files. This is an important idea in linking, for example if I have two identical functions (assume that they are strongly defined as in my example, i.e. with a function body), and I don't label them as static, it will cause a linker error. But if I declare them as static, then the linker knows they are local to the file, and will treat it as OK.
file1
file2
OR
file1
file2
The static keyword can be used along with extern to manipulate the "visibility" of functions to the linker to deal with it. If you plan on using a non-member function in other files, you shouldn't label it as static.