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
- Our program starts from main function.
- All the functions are called directly or indirectly through main function.
- Suppose we have used variable in our program then before using variable we must declare variable , similarly we must declare function before using it.
- Declaration of function in C is called as prototype declaration.
- Function Declaration is also called as Function Prototype.
- It Provide following information to the Compiler
- Name of the Function
- Return Type [ Optional / Default-Integer ]
- 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
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 :
- If function Definition is Written after main then and then only we write Prototype Declaration in Global Declaration Section
- 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 ?
- 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.
- During Code Generation Phase of Compiler it may face issue of backward reference.
- If we write prototype declaration then -
- Prototype declaration tells compiler that we are going to define this function somewhere in the program.
- Compiler will have prior information about function.
- As Compiler have prior information , During function calling compiler looks forward in the program for the function definition.
- If we don’t write prototype declaration then -
- Compiler don’t have any reference of Function.
- Compiler don’t have prior information of that function.
- Compiler gets confused.
- Compiler interpret it as unknown reference and throws error .
Quick Reference :
- Our Article : How to Call a function ?
- Wikipedia Article : Prototype Declaration in C