C Functions Declaration

Functions Declaration

A function consist of two parts:


  • Declaration, where we specify the function's name, its return type, and required parameters (if any).
  • Definition, which is nothing but the body of the function (code to be executed)

 

The basic structure of a function is,

return_type functionName() { // declaration
  // body of the function (definition)
}


For code optimization and readability, it is often recommended to separate the declaration and the definition of the function.


And this is the reason why you will often see C programs that have function declarations above the main() function, and function definitions below the main() function. This will make the code better organized and easier to read.

 

This is how it is done,

#include <stdio.h>
 
// Function declaration
void func();
 
int main()
{
    func(); // calling the function
    return 0;
}
 
// Function definition
void func()
{
    printf("This is the definition part.");
}


Output:

This is the definition part.