confused with prototyping

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

Post a Reply

Replies

Oldest  Newest  Rating
0 Scott Walker · November 6, 2015
Yes as long as it is prototyped before the main function.
0 Shreyan Bakshi · November 28, 2015
Yes. 

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 :

#include<iostream>
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;


}
  • 1

C++

107,222 followers
About

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

Links
Moderators
Bucky Roberts Administrator