Function Prototype and Definition

Printable View

• 01-21-2005
XDarklytez
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; }```
• 01-21-2005
sand_man
ok
Code:

```int Area(int length, int width); //function prototype //should be int Area(int, int); //function prototype```
Function prototype doesnt need variable names. names are given in the implementation.
Code:

```int Area(int 1, int w) {     return 1 * w; } //should be int Area(int l, int w) {     return l * w; }```
you had the number 1 instead of the letter l
• 01-21-2005
Salem
> int Area(int 1, int w)
It looks like a ONE(1) to me, rather than the letter L(l)
• 01-21-2005
Salem
> //should be
> int Area(int, int); //function prototype
You mean CAN be.

Sure you can, but when you have masses of header files (and maybe not even the source code), which would you prefer to see.
char *strstr( char *haystack, char *needle );
or
char *strstr( char*, char* );

It's dead easy to see what the parameters for the first one should be without having to go look up some additional information in yet another place.
• 01-21-2005
elad
If you use parameter names in addition to parameter types in the function prototypes I suggest you use the same names in the first line of the function definition as well.

int Area(int length, int width);
int Area(int 1, int w)
• 01-21-2005
DougDbug
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.