Function Prototypes in C

Function Prototypes require that every function which is to be accessed should be declared in the calling function. The function declaration, that will be discussed earlier, will be included for every function in its calling function.

Example for the function prototype as follows:

/*Program to calculate the square of a given integer using the function prototype*/
#include <stdio.h>
main ( )
{
int n , sq ;
int square (int ) ; /* function prototype */
printf (“Enter a number to calculate square value”);
scanf(“%d”,&n);
sq = square(n); /* function call with parameter passing */
printf (“\nSsquare of the number is : %d”, sq);
}
/* square function */
int square (int no) /*passing of argument */
{
int result ; /* local variable to function square */
result = no*no;
return (result); /* returns an integer value */
}

OUTPUT

Enter a number to calculate square value: 5
Square of the number is: 25

Points to remember:

  • Function prototype requires that the function declaration must include the return type of function as well as the type and number of arguments or parameters passed.
  • The variable names of arguments need not be declared in the prototype.
  • The major reason to use this concept is that they enable the compiler to check if there is any mismatch between function declaration and function call.

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.

%d bloggers like this:
?>