A global function is a function defined in a C file before the main() function. Even the main() function is a global function but the main() function is not the focus in this article. In the language C, programmer-defined functions are usually global functions and simply referred to as functions.
A global function can be called in any scope in its file. In order for a global function to be seen in a different file, its prototype has to be declared in that different file (see below.) To prevent a function defined in one file from being seen in a different file, precede the definition with the reserved word, static. With that, the static function would be a global function only in its own file and will not be seen in another file.
So, a global function defined in otherFile.c can be seen anywhere in otherFile.c. For it to be seen in mainFile.c, its prototype has to be declared in mainFile.c (see below.) Its definition in otherFile.c should not be preceded by “static”. To prevent it from being seen in mainFile.c, its definition in otherFile.c has to be made staticremoved a comma by preceding it with the word, static.
This article illustrates static function in the computer language, C beginning with the meaning of function prototype and its use in a C program of two or more files.
Compilation of the files, mainFile.c and otherFile.c, can be done with the following Bash terminal command:
temp.exe is the name of the resulting single executing file.
C Function Prototype
Consider the global function definition:
The prototype for this function is:
It is the function signature, ending in semicolon.
Now, let the content of the otherFile.c be:
Assume that the file, otherFile.c has only the definition of the global function, fn1(). Now let the content of the file, mainFile.c, be:
char* fn1(char* stri);
char* str = fn1("seen");
It begins with the inclusion of the <stdio.h> header (library). This is followed by the prototype declaration of the function, fn() of the other file.
This main file does not have the definition of any global function other than the default main() function. In the main function, the first statement calls function fn1() defined in the other file. This call would not be effective if the prototype of fn1() was not declared in this file, mainFile.c.
If the reader has coded the above two files, then he can compile the program into one executable file with the following command:
pressing the Enter key at the end of the line. The output should be, “seen”.
The mainFile.c file can be modified as follows:
Now, there are two global functions in the file, mainFile.c. The names of the functions are fn() and main(). dfn() is a global function. It has been seen in the main() function local scope, by a call. In the language C, a global function in the global scope, such as fn(), is simply referred to as a function. Also, the global function, fn1() in the file, otherFile.c is simply referred to, as a function.
If the two files are re-compiled into temp.exe, then the outout will be:
Global seen in its file.
Note: the function definition for fn() can also be made to be seen in the other file, otherFile.c. To achieve that have its prototype declaration in otherFile.c, as follows:
ending with a semicolon. This activity, is left as an exercise for the reader.
Static Function Proper
From the above discussion, a function can be seen in any scope (anywhere) in its file. It can also be seen in a different file, for the same program, if its prototype is declared there. In order to prevent the definition of the file from being seen in a different file, make the definition static by preceding it with the reserved word, static. Even if its prototype is declared in the different file, it will still not be seen in the different file. The program files will not even compile. To illustrate this, let the content of the file, otherFile.c be:
This is the same content in the file, otherFile.c, as before, but with the preceding reserved word, static. The content of the mainFile.c file, remains the same. If an attempt is made to compile the program files with the command,
the compiler would issue an error message, meaning the program did not compile. Even if the prototype declaration in the different file, is also preceded by static, the program files will still not compile.
A function definition in the C language, is a global function. This function can be called (seen) in any scope in its file. In order for this function to be seen in a different file, such as the main file, its prototype declaration has to be typed in that different file. To prevent this function from being seen in the different file make its definition static by preceding it with the reserved word, static.