Function declaration, definition and prototype

Function prototype or Function declaration 

A function prototype is declaration of the function which tells the compiler about a function’s name, return type , number of parameter and types of parameters. It is used to say the compiler about existence of a function.

A function prototype is not required if a function defined before using it, Here the function definition will act as function prototype too. Next we will study about function definition.

Both C program given below will compile and give same output.

  1. #include<stdio.h>
  2.   void gt_func1( );   /* Function prototype */
  3.   int main( )
  4.   {
  5.       gt_func1( ); /* Function call */
  6.       printf(” Returning from function gt_func1 \n”);
  7.        return 0;
  8.   }
  9.   void gt_func1( )
  10.   {
  11.       printf(“Welcome to\n”);
  12.   }

In below program, called function defined before using it.

  1. #include<stdio.h>
  2. void gt_func1( )
  3.  {
  4.       printf(“Welcome to\n”);
  5.  }
  6.   int main( )
  7.   {
  8.       gt_func1( ); /* Function call */
  9.       printf(” Returning from function gt_func1 \n”);
  10.        return 0;
  11.   }



Function definition

Actual body of function is called function definition. The general form of  a function definition is given below.

return_type  function_name (parameter1, parameter2 ……., parameter N)
/* function body */
}The smallest function is:-

gt_function()  { }

This function is doing nothing and returns nothing.

A function definition consists following things :-

Function Name – This is the actual name of the function.

Function Parameters – When a function is called, we pass a  variable or value to the parameter. This is called actual parameter or argument. This is optional part in function.

Function Body – The function body contains a collection of statements inside function parenthesis.

Return Type – A function  may return a value or may not. If the return type is not mentioned then int is assume. The return type of the function is the data type of  the value the function returns. Some function doesn’t return anything. In this case the return type is keyword ‘ void ‘.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.