Function Prototype and Definition
I am really a newbie to C++ and I've been reading the Sams Teach Yourself C++ in 21 Days book. There is one example program in the book demonstrating the use of function prototypes, and I've typed in the code exactly as it is in the book, but I got some errors. Firstly I got a syntax error before numeric constant in line 28, the one with the function definition. Then, I got another error " 'w' undeclared (first use this function) in line 30. Anyone can tell me what is wrong?
Code:
// Listing 5.1 - demonstrates the use of funtion prototypes
#include <iostream>
int Area(int length, int width); //function prototype
int main()
{
using std::cout;
using std::cin;
int lengthOfYard;
int widthOfYard;
int areaOfYard;
cout << "\nHow wide is your yard? ";
cin >> widthOfYard;
cout << "\nHow long is your yard? ";
cin >> lengthOfYard;
areaOfYard = Area(lengthOfYard, widthOfYard);
cout << "\nYour yard is ";
cout << areaOfYard;
cout << " square feet\n\n";
return 0;
}
int Area(int 1, int w)
{
return 1 * w;
}
You can download the code.
XDarklytez,
I do encourage you to hand-type-in all of the example programs, but when you run into a problem, you can download the code from LibertyAssociates.com
Quote:
...I suggest you use the same names in the first line of the function definition as well.
elad makes a good point, but the author is making an important point too. The variable names don't have to match.
More importantly, the variables in the calling statement (lengthOfYard, widthOfYard) don't have to match. (We're passing by value.) When you use one of the standard library functions... i.e. something from <cmath> or <cstring>, you will be passing-in your variables that you have named into a function that you did not write. They will almost never match the variable-names inside the function.