confused with prototyping

+1 jacques iragena · October 30, 2015
Can we type the body of  printcrap after the main function ??

Post a Reply


Oldest  Newest  Rating
0 Shreyan Bakshi · November 28, 2015

But, to do this you need to prototype it before the main function.
This is done so that when the compiler comes across the function in main(), it knows that such a function is actually defined by the programmer later on in the program. This stops it from providing an error do to an undefined function.

For example :

using namespace std;

int avg(int a, int b);       \\ This is the prototype for the Average function (it doesn't include the body of the function).

int main()                                                                                                                                                  \\ main() begins here.

int num1, num2 , res;

cout <<"Enter the 2 numbers : \n";
cin >> num1 >> num2;

res = avg(num1, num2);

cout << "The average of " << num1 << " and " << num2 << " is : " << res << endl;

}                                                                                                                                                                   \\ main() ends here.

int avg(int a, int b) {          \\ This is the actual Average function (it includes the body of the function).

int s = (a+b)/2;

return s;

0 Scott Walker · November 6, 2015
Yes as long as it is prototyped before the main function.
  • 1



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator