What is the advantage of a static function?
Asked Answered
L

5

14

The declaration below:

static int *foo();

declares foo as a static function returning a pointer to an int.

What's the purpose of declaring a function as static ?

Lewallen answered 18/10, 2011 at 20:54 Comment(0)
I
18

The function's name isn't visible outside the translation unit (source file) in which it's declared, and won't conflict with another function foo in another source file.

In general, functions should probably be declared static unless you have a specific need to call it from another source file.

(Note that it's only the name that's not visible. It can still be called from anywhere in the program via a pointer.)

Imf answered 18/10, 2011 at 20:58 Comment(2)
What is the difference between marking a function as static and simply not including it in the header? Won't omitting it from the header have the same effect? Or, is there a reason to not put something in the header and also not mark it as static?Basophil
@Alex: Including its declaration in the header makes it visible to the compiler, and thereby to other source files that include that header. Making it non-static makes its name visible to the linker; other source files can access it by declaring it themselves. As a matter of style, either (a) declare it static, or (b) declare it non-static and include its declaration in the corresponding .h file. (If C had a full-fledged module system, guidelines like this wouldn't be necessary.)Imf
F
6

It prevents other translation units (.c files) from seeing the function. Keeps things clean and tidy. A function without static is extern by default (is visible to other modules).

Francinefrancis answered 18/10, 2011 at 20:56 Comment(0)
M
4

From the C99 standard:

6.2.2 Linkages of identifiers

If the declaration of a file scope identifier for an object or a function contains the storage-class specifier static, the identifier has internal linkage.

and

In the set of translation units and libraries that constitutes an entire program, each declaration of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each declaration of an identifier with internal linkage denotes the same object or function. Each declaration of an identifier with no linkage denotes a unique entity.

Mishnah answered 18/10, 2011 at 21:2 Comment(0)
O
3

Declaring a function as static prevents other files from accessing it. In other words, it is only visible to the file it was declared in; a "local" function.

You could also relate static (function declaration keyword, not variable) in C as private in object-oriented languages.

See here for an example.

Outrider answered 18/10, 2011 at 20:58 Comment(3)
The question is about c. What's a "class"?Imf
i am more concern with c ...i dont know class & c++Lewallen
@Keith: Apologies - been coding solely in Java for the past month.Outrider
S
3

Marking a function or a global variable as static makes it invisible to the linker once the current translation unit is compiled into an object file.

In other words, it only has internal linkage within the current translation unit. When not using static or explicitly using the extern storage class specifier, the symbol has external linkage.

Scorpaenoid answered 18/10, 2011 at 21:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.