Function Declaration in C Programming

A function declaration in C tells the compiler about function name, function parameters and return value of a function. The actual body of the function can be defined separately.

Like variable in C, we have to declare functions before their first use in program.

Syntax of Function Declaration

return_type function_name(type arg1, type arg2 .....);

For Example
  • Declaration of a function with no input parameters
    void printMatrix();

  • Declaration of a function with one input parameter and integer return type
    int isEvenNumber(int num);

  • Declaration of a function with multiple input parameter and integer return type.
    int getSum(int num1, int num2);

  • Declaration of a function with no input parameters and integer return type.
    int getRandomNumber();

Important Points about Function Declaration.
  • Function declaration in C always ends with a semicolon.

  • By default the return type of a function is integer(int) data type.

  • Function declaration is also known as function prototype.

  • Name of parameters are not compulsory in function declaration only their type is required. Hence following declaration is also valid.
    int getSum(int, int);

  • If function definition is written before main function then function declaration is not required whereas, If function definition is written after main function then we must write function declaration before main function.

C program to show function definition before main

#include<stdio.h>
#include<conio.h>

/* 
 *  Function Definition before main 
 *  In this case function declaration is not required
 */
 
int getSum(int a, int b) {
   return a+b;
}

int main(){
   int a, b, sum;
   printf("Enter two numbers\n");
   scanf("%d %d", &a, &b);
   /* Calling getSum function */
   sum = getSum(a, b);
   printf("SUM = %d", sum);
   
   getch();
   return 0;
}

Program Output
Enter two numbers
2 6
SUM = 8

C program to show function definition after main

#include<stdio.h>
#include<conio.h>

/* Function Definition after main 
   In this case function declaration is required */
int getSum(int a, int b);

int main(){
   int a, b, sum;
   printf("Enter two numbers\n");
   scanf("%d %d", &a, &b);
   /* Calling getSum function */
   sum = getSum(a, b);
   printf("SUM = %d", sum);
   
   getch();
   return 0;
}

int getSum(int a, int b) {
   return a+b;
}

Output
Enter two numbers
6 7
SUM = 13