Prototype Declaration : Declaring Function in C Programming

Declaring Function in C Programming : Prototype Declaration of function in C

Prototype declaration is necessary in order to provide information to the compiler about function , about return type,parameter list,function name etc.

Topic Introduction : Prototype Declaration of function in C

  1. Our program starts from main function.
  2. All the functions are called directly or indirectly through main function.
  3. Suppose we have used variable in our program then before using variable we must declare variable , similarly we must declare function before using it.
  4. Declaration of function in C is called as prototype declaration.
  5. Function Declaration is also called as Function Prototype.
  6. It Provide following information to the Compiler
    1. Name of the Function
    2. Return Type [ Optional / Default-Integer ]
    3. Argument List / Parameter List

Note : Important Points about Prototype Declaration

  • Prototype declaration always ends with semicolon.
  • Parameter List is Optional.
  • Default Return Type is Integer.

Diagram : Prototype Declaration

C Programming Prototype Declaration Section

Syntax : Prototype Declaration

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

Some Valid Examples : Prototype Declaration

Function with two integer arguments and integer as return type

int sum(int,int);

Function with integer argument and integer as return type

int square(int);

Function with no argument and no return type

void display(void);

Function with no argument and integer as return type

int getValue(void);

Facts about Prototype Declaration :

  1. If function Definition is Written after main then and then only we write Prototype Declaration in Global Declaration Section
  2. If function Definition is written above the main function then , no need to write Prototype Declaration

Explanation of Prototype Declaration With Example

Case 1 : Function Definition Written Before Main

#include<stdio.h>
void displayMessage()
{
printf("www.c4learn.com");
}
void main()
{
displayMessage();
}

Case 2 : Function Definition Written After Main

#include<stdio.h>
//Prototype Declaration
void displayMessage();
void main()
{
displayMessage();
}
void displayMessage()
{
printf("www.c4learn.com");
}

Detailed Explanation : Why we have to write prototype declaration in 2nd Example ?

  1. Program Execution always starts from main , but during lexical analysis (1st Phase of Compiler) token generation starts from Left to Right and From Top to Bottom.
  2. During Code Generation Phase of Compiler it may face issue of backward reference.
  3. If we write prototype declaration then -
    1. Prototype declaration tells compiler that we are going to define this function somewhere in the program.
    2. Compiler will have prior information about function.
    3. As Compiler have prior information , During function calling compiler looks forward in the program for the function definition.
  4. If we don’t write prototype declaration then -
    1. Compiler don’t have any reference of Function.
    2. Compiler don’t have prior information of that function.
    3. Compiler gets confused.
    4. Compiler interpret it as unknown reference and throws error .

Quick Reference :

  1. Our Article : How to Call a function ?
  2. Wikipedia Article : Prototype Declaration in C