50

What is the utility of having static functions in a file ?

How are they different from having global functions in a file ?

static int Square(int i)
{
   return i * i;
} 

vs

int Square(int i)
{
   return i * i;
}
Arun
  • 3,138
  • 4
  • 30
  • 41

4 Answers4

58

What is the utility of having static functions in a file?

You can use these functions to provide shared implementation logic to other functions within the same file. Various helper functions specific to a file are good candidates to be declared file-static.

How are they different from having global functions in a file?

They are invisible to the linker, allowing other compilation units to define functions with the same signature. Using namespaces alleviates this problem to a large degree, but file-static functions predate namespaces, because they are a feature inherited from the C programming language.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 1
    It all became clear to me when I read that global functions are external by default, that is "void fun() {}" is identical to "extern void fun() {}"... still, if you don't put them in a header they can only be used within implementation file in which they are defined (below the point at which they are defined). – Simon Knapp Jan 31 '14 at 00:27
  • 5
    @Simon Knapp they can be used in different transition units by using a prototype. – Cem Kalyoncu Apr 03 '14 at 13:33
27

A static function simply means that the linker cannot export the function (i.e. make it visible to other translation units). It makes the function "private" to the current translation unit. It is the same as wrapping the function in an anonymous namespace.

namespace {

    int Square(int i)
    {
       return i * i;
    } 

}

Generally, using an anonymous namespace is the preferred C++ way of achieving this, instead of using the static keyword.

Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
  • 13
    Why is it preferred? It seems more verbose to me. – rozina Jul 11 '16 at 12:38
  • 1
    Yeah if you were in the middle of a 1000 line file it's easier to check whether a function is static than whether it's in a namespace, especially if you don't indent namespaces – Volper Nov 21 '20 at 14:47
  • Personally: because you're writing C++ code, not C, as it is a feature inherited from C. I tend to avoid features inherited from C. For example, I'd rather use `std::from_chars` rather than `atoi`, while the former being more verbose. – Marc Dirven Nov 02 '22 at 10:21
  • That's not really a reason @MarcDirven, just a preference – Jerem Nov 14 '22 at 08:06
  • @Jarem Being good C++ programmer is to understand the idioms. – Marc Dirven Nov 14 '22 at 11:38
4

Static functions are visible on the file where they were defined only. You can't refer to them outside of that particular file.

Read more here

4

In a word, linkage. static functions have internal linkage, that is, they aren't visible outside of their translation unit.

Yuushi
  • 25,132
  • 7
  • 63
  • 81