Function Prototype ( Function Declaration) in C

A function prototype tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately. The syntax for function declaration is :
return_type function_name (parameter_list);
For example :
int add( int a, int b);
The above statement declare the prototype of function add with two parameter of type int. Since the  parameter names are not important in function declaration, just their type is required, then we can also write the above prototype as :
int add( int, int );
The function declaration is required when we define a function after main() function or another source file. In such cases, we should declare the function at the top of the file calling the function.

Now lets see some example of it.

Example 1 :
#include <stdio.h>

int add(int, int);

int multiply(int, int);

int main() {
  int val;

  val = add(8, 8);
  printf("Addition : %d\n", val);

  val = multiply(8, 8);
  printf("Multiplication : %d\n", val);

  return 0;
}

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

int multiply(int a, int b) {
  return a*b;
}
Output :

Addition : 16
Multiplication : 64

Example 2 :

In this example the functions are defined in another source file.

Filename : func.c
int add(int a, int b) {
  return a+b;
}

int multiply(int a, int b) {
  return a*b;
}
Filename : main.c
#include <stdio.h>

int add(int, int);

int multiply(int, int);

int main() {
  int val;

  val = add(8, 8);
  printf("Addition : %d\n", val);

  val = multiply(8, 8);
  printf("Multiplication : %d\n", val);

  return 0;
}
To compile the above code on gcc use below command :
 gcc main.c func.c -o test
Output :

Addition : 16
Multiplication : 64


Next Topic :