What are Static functions and where are they used?

Static functions are similar to the static variables as we have discussed the scope and use of static variables in the previous tutorial I will not explain those concepts here again. Let’s now discuss first what static functions are.

What are Static functions?

In C programming language all the functions defined are declared as global by default. But if we want to change the type of a function we need special keywords to do so. Static functions can be defined by using the keyword static before the return type of the function.

static void static_func ();

The scope of a static function is restricted to the object file. It means that the function can only be used or called in the file in which the function is defined and declared. If we define a static function in one file named “file_one.c”, for instance, and use/call that function in another file named “file_two.c” then we will get an error. Let’s discuss the functionality of a static function with the help of example codes.

Static Function Example 1

The example code shown in the figure below uses a static function.

#include<stdio.h>
/* Inside Static_function.c */
 void static_func(void)
{
printf("Inside the static function.");
}

The static function named “static_func” called in the main function will not produce any error because the function is called within the scope of the static function which is the file in which it is defined i.e. Static_function.c. The output of this function is shown in the figure below,

Output:

static functions examples in c 1

Let’s now exercise a negative scenario of using a static function.

How Not to use Static Functions?

In this example we will create 2 different files and declare the function in 1 file while we will call the same static function in another file at let’s see what will happen when we do so.

There is one file named “Static_function.c” shown in the figure below in which static function is defined.

#include<stdio.h>
/* Inside Static_function.c */
 void static_func(void)
{
printf("Inside the static function.");
}

And then there is another file named main.c in which the main function is defined and the static function is called as shown in the figure below.


#include<stdio.h>
/* Inside main file */
int main(void)
{
static_func();
return 0;
}

Compiling this main code will result in compilation error because the static function is called outside its scope. The compilation error of this code is shown in the figure below,

Error:

static functions examples in c 2

Uses of Static Functions

The next thing to discuss in this tutorial is where to use static functions. To be precise and brief, we use static functions when we want to restrict our piece of code to a specific file. Doing so will not allow code sharing in multiple files. Static functions are not recommended if we want to reuse the code in different files. We can also use static functions if we want to use the same names for different functions in different files. This is not a good practice however at a lower level some developers do so to save themselves from the efforts of remembering the names of multiple functions.

Related Articles:

  1. C Program Compilation steps & Example with GCC Linux
  2. Introduction to Pointers
  3. How to find an array size without using sizeof()
  4. Pointer Arithmetic, Pointer Size, and Pointer Type
  5. In which order function parameters are evaluated in C/C++?
  6. Scope of global and static variables in C/C++
  7. Difference between Structure and Union in C
  8. C Program to remove duplicate elements from an array
  9. Difference Between void main(), int main() and void main(void) in C/C++
  10. Return Multiple Values from a function
  11. What Static functions and where are they used?

Leave a Reply