Function Declaration
A function declaration in C is a statement that provides the compiler with the information it needs to verify that a function is defined correctly and to generate code to call the function. A function declaration specifies the function's name, return type, and parameter list.
A function declaration is also known as a function prototype, and it is typically placed at the beginning of a C program, before the main function.
Here is an example of a function declaration in C:
#include <stdio.h>
int add(int x, int y); // function declaration
int main()
{
int result = add(3, 4);
printf("Result: %d\n", result);
return 0;
}
int add(int x, int y) // function definition
{
return x + y;
}
In this example, we have declared the function add before the main function using a function declaration. The function declaration specifies the function's name, return type, and parameter list, but it does not include the function's implementation.
The function is then defined after the main function, where the implementation is provided. The function definition includes the function's name, return type, parameter list, and the code that is executed when the function is called.
Function declarations are used to inform the compiler about the existence and signature of a function, and they are necessary in C because functions can be defined in any order. Function declarations allow you to call a function before it is defined, as long as the function is defined before the program is executed.
Function declarations are also useful when writing large programs, as they allow you to define functions in separate source files and link them together when the program is compiled. This helps to organize and structure the code in a modular way.