Thread: Function Prototype and Definition

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    1

    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;
    }

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int Area(int 1, int w)
    It looks like a ONE(1) to me, rather than the letter L(l)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > //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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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)
    You're only born perfect.

  6. #6
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Lightbulb 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

    ...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.
    Last edited by DougDbug; 01-21-2005 at 01:55 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C interview questions
    By natrajdreams in forum C Programming
    Replies: 7
    Last Post: 12-12-2010, 12:40 PM
  2. Why put the prototype inside the function definition?
    By Overworked_PhD in forum C Programming
    Replies: 14
    Last Post: 01-11-2010, 01:48 PM
  3. Replies: 2
    Last Post: 04-22-2009, 02:35 AM
  4. A Function prototype I don't recognize...
    By DominicTrix in forum C++ Programming
    Replies: 4
    Last Post: 04-12-2003, 04:46 PM
  5. error LNK2001: unresolved external symbol
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 07-12-2002, 08:45 PM