What is function prototype?

Long Answer
Views 852

Answer:

The ANSI C standard introduces function prototypes. An example is given below. It also allows function definitions to be written in the same form as the new prototypes.

Code:

#include
double minimum(double, double);
/* prototype of minimum() */
int main()
{
printf("%f
", minimum(1.23, 4.56));
return 0;
}

double minimum(double x, double y)
/* definition of minimum() */
{
if (x < y)
  return x;
else
  return y;
}

Output:

1.230000
Press any key to continue . . .

The use of prototypes or the new style function definition allows the compiler to check that the parameters to a function are sensible (not necessarily the same), as well as checking the return type of the function; but only if the definition or prototype appears before the function call.
ANSI C draws a distinction between the following two statements. The first is a function declaration stating that fred takes an, as yet, unspecified number of parameters. The second is a function prototype which states that jim takes no parameters


double fred(); /* declaration */
double jim(void); /* prototype */

Related Articles:

This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of C Programming Language, click the links and dive deeper into this subject.

Join Our telegram group to ask Questions

Click below button to join our groups.