C function prototype declaration



We have learnt about the basics concept and working of function and defining the function. In this tutorial we will br learning function prototype declaration using C programming

Function prototype declaration in C Programming

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

Important Points :

  1. Our program starts from main function. Each and every function is called directly or indirectly through main function
  2. Like variable we also need to declare function before using it in program.
  3. In C, declaration of function is called as prototype declaration
  4. Function declaration is also called as function prototype

Points to remember

Below are some of the important notable things related to prototype declaration –

  1. It tells name of function,return type of function and argument list related information to the compiler
  2. Prototype declaration always ends with semicolon.
  3. Parameter list is optional.
  4. Default return type is integer.

Pictorial representation

C Programming Prototype Declaration Section

Syntax

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

prototype declaration comprised of three parts i.e name of the function,return type and parameter list

Examples of prototype declaration

Function with two integer arguments and integer as return type is represented using below syntax

int sum(int,int);

Function with integer argument and integer as return type is represented using below syntax

int square(int);

In the below example we have written function with no argument and no return type

void display(void);

In below example we have delclared function with no argument and integer as return type

int getValue(void);

Positioning function 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

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");
}

Need of prototype declaration

  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.

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.

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 and 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