1. Sample from Book How to Program C++ by Deitel & Deitel
#include <iostream>
int square ( int ); // function prototype
int main()
{
for ( int x = 1; x <= 10; x++ )
cout << square (x) << " ";
cout << endl;
return 0;
}
// function definition
int square ( int y )
{
return y * y;
}
2. my own version based on #1
#include <iostream>
using namespace std;
int square ( int x )
{
int r;
r = x * x;
return (r);
}
int main ()
{
for ( int x = 1; x <= 10; x++ )
{
cout << square ( x ) << " " << endl;
}
cin.get();
return 0;
}
I was confused in the first place (by the book), so I looked it up on the website.
Functions (I)
You can tell that my version (#2) is written in the style suggested by the tutorial.
My questions are:
1. For a simple program, which way do you prefer?
2. For a large program, or real world programming, which way do you prefer?
3. I understand how function work, but is not clear with #1 original code. The program begins with int main and when the compiler reaches square ( x ), which one does it return to? Is it the prototype first, or the definition second? I am not quite clear with those two lines.
Thank you very much.


Sign In
Create Account


Back to top









