Syntax
<function data_type> static <function_ name> ( ){}
or
static int function_name ()
{
// define the statement to be executed
}
Here, the static keyword is placed in front of the function_name to designate the function as static.
Some importanLogic Practices of static function
- A static keyword can be used before the name of a function to make it static.
- However, the scope of the static function is throughout the program only. It means the static function can be called inside the same program or file.
- If we try to access the static function from another file, it throws an error.
- We can make a global function as the static by adding the static before the function name of a program.
Program to call a global function inside another file
In this software, we are required to generate two files, file1.c and file2.c. Subsequently, we establish a universal function within the file1.c as illustrated underneath.
File1.c
int add(int x, int y) // this function is called to the file2.c
{
int sum; // declare the sum variable
sum = x + y; // store the sum of two number in sum
return sum;
}
After declaring the global function within file1.c, we proceed to establish a new file named file2.c where we proceed to invoke the global function add.
File2.c
#include <stdio.h>
#include <stdlib.h>
#include "file1.c" // file1.c is use as a refernce file
int add(int x, int y); // define the global function
int main()
{
int n1, n2, res; // define local variables
printf (" Enter any two numbers: ");
scanf ("%d %d", &n1, &n2); // accepts two numbers
res = add(n1, n2); // call global function from "file1.c"
printf (" The sum of the two numbers is: %d", res);
return 0;
}
Output
Enter any two numbers: 5
20
The sum of the two numbers is: 25
As demonstrated in the preceding code snippet, we generate a pair of files, file1.c and file2.c. Within file1.c, we establish a global function add which is invoked within file2.c. Subsequently, file2.c recognizes file1.c as the source file housing the global function definition and invokes it within the main function to compute and return the sum of two integer values.
Program to call a static function inside another file
Let's develop a script to convert a global function into a static function by placing the static keyword in front of the function definition and then invoke it in file2.c.
File1.c
static void add(void) // It is a static function
{
printf (" I am a static function");
}
Now, we will generate a new file named file2.c and invoke the static function to examine its scope.
File2.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf(" I am a main() function.");
myFun(); // call static function
return 0;
}
Output
Undefined reference to myFun() function.
When the program mentioned above is compiled, an error occurs stating "undefined reference to myFun. Due to the static nature of the function myFun, its object files are restricted to accessing it exclusively, thereby preventing other functions from doing so.
Program to call a static function in same object file or function
Let's develop a program showcasing the utilization of the static function in the C programming language.
File1.c
#include <stdio.h>
#include <stdlib.h>
// define the static function
static int mySqr( int num)
{
return num * num;
}
int main()
{
int n1, res;
printf (" Enter an integer number: ");
scanf ("%d", &n1);
res = mySqr(n1); // call static function
printf(" The square of the %d is %d.", n1, res);
return 0;
}
Output
Enter an integer number: 25
The square of the 25 is 625
In the preceding code snippet, a static function is defined within the object file File1.c. Within File1.c, the main function invokes the static function mySqr to calculate and return the square of a specified number. The static function operates effectively due to its limited scope within the object file or program.
Program to call multiple static function in same file
Let's develop a script to define and invoke various static functions within the main function.
Program.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
// define the static function
static int mySqr( int num)
{
return num * num;
}
static void myfun2(void)
{
printf ("\n I am inside the static function.");
}
static int myFun3(int num2)
{
return sqrt(num2);
}
int main()
{
int n1, res;
printf (" Enter an integer number: ");
scanf ("%d", &n1);
res = mySqr(n1); // call static function
printf(" The square of the %d is %d", n1, res);
myfun2(); // call static function
res = myFun3(n1);
printf(" \n The square root of the %d is %d", n1, res);
return 0;
}
Output
Enter an integer number: 36
The square of the 36 is 1296
I am inside the static function.
The square root of the 36 is 6
In the previous code snippet, we define several static functions within a single object file. Each of these static functions is then invoked within the main function to provide a return value.