Thread: Reguarding 04 tutorial ( prototypes )

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    8

    Reguarding 04 tutorial ( prototypes )

    Code:
    #include <iostream>
    
    using namespace std;
    
    int mult ( int x, int y );
    
    int main()
    {
      int x;
      int y;
      
      cout<<"Please input two numbers to be multiplied: ";
      cin>> x >> y;
      cin.ignore();
      cout<<"The product of your two numbers is "<< mult ( x, y ) <<"\n";
      cin.get();
    }
    
    int mult ( int x, int y )
    {
      return x * y;
    }
    The above snippet is from http://www.cprogramming.com/tutorial/lesson4.html ...

    Why are prototypes used? wouldn't it be cleaner too write the functions before the required one? I'm confused on why they are needed.

    If someone could explain I didn't see it mentioned in the tutorial of why so I ask here.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because at the point when the compiler sees your call to mult, it has no idea what the function looks like, what it returns and what arguments it expects.
    Therefore we add a prototype (a declaration) so the compiler knows these things and can point out if you make errors in your code (and also generate correct code!).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    62
    prototypes can often be much cleaner then just placing the function in front of the place you want to use it at. When developing big projects, it is a good idea to split up your program into many separate files, this allows you to keep track of what code is where. Prototypes in a header file allow a quick reference to what functions are in a file, and the safety feature of not having to know which order the compiler compiles your file.

    (for example, it can compile main.cpp and notmain.cpp in whatever order it likes while main.cpp is still able to access any function in notmain.cpp)

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Cogman View Post
    (for example, it can compile main.cpp and notmain.cpp in whatever order it likes while main.cpp is still able to access any function in notmain.cpp)
    In fact, regardless of order of compilation, if you use separate source files, you need a prototype to tell the compiler what the function(s) in the other source file(s) look like. And as soon as your project gets a bit bigger, you will want to start splitting it into multiple files. A file should cover "one area" of your program. In C++ it's very common to have one .cpp and one .h file for each class - where the .h file contains the declaration (that is, the member variables and prototypes of the member functions), and the .cpp file contains the implementation of the member functions.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    8
    Thanks, I got alittle bit wiser

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    8

    Unhappy Another Prototype Problem:

    Here i'm trying to finish the 05 lessons by writing up the functions for the voids in their code. But it complains on me, this is what I got so far:


    Code:
    #include <iostream>
    
    using namespace std;
    
    int playgame();
    int loadgame();
    int playmultiplayer();
    
    int main()
    {
      int input;
      
      cout<<"1. Play game\n";
      cout<<"2. Load game\n";
      cout<<"3. Play multiplayer\n";
      cout<<"4. Exit\n";
      cout<<"Selection: ";
      cin>> input;
      switch ( input ) {
      case 1:            // Note the colon, not a semicolon
        playgame();
        break;
      case 2:            // Note the colon, not a semicolon
        loadgame();
        break;
      case 3:            // Note the colon, not a semicolon
        playmultiplayer();
        break;
      case 4:            // Note the colon, not a semicolon
        cout<<"Thank you for playing!\n";
        break;
      default:            // Note the colon, not a semicolon
        cout<<"Error, bad input, quitting\n";
        break;
      }
      cin.get();
    }
    
    int playgame() {
      cout<<"Yeaha Baby we're playing!\n";
    }
    int loadgame() {
      cout<<"Bummer.. you cheating bastard, loading a game are you?\n";
    }
    int playmultiplayer() {
      cout<<"Let the multiplayer mayhem begin...\n";
    }
    I changed void prototypes too int
    defined the functions below main

    It works too compile, and run but the compiler complains and was wondering if i'm doing something bad....

    the compile error:

    g++ -c -Wall lesson2.cpp -o lesson2.o
    lesson2.cpp: In function ‘int playmultiplayer()’:
    lesson2.cpp:47: warning: control reaches end of non-void function
    lesson2.cpp: In function ‘int loadgame()’:
    lesson2.cpp:44: warning: control reaches end of non-void function
    lesson2.cpp: In function ‘int playgame()’:
    lesson2.cpp:41: warning: control reaches end of non-void function
    g++ lesson2.o -o hello



    Originally it looked like this:

    Code:
    #include <iostream>
    
    using namespace std;
    
    void playgame();
    void loadgame();
    void playmultiplayer();
    	
    int main()
    {
      int input;
      
      cout<<"1. Play game\n";
      cout<<"2. Load game\n";
      cout<<"3. Play multiplayer\n";
      cout<<"4. Exit\n";
      cout<<"Selection: ";
      cin>> input;
      switch ( input ) {
      case 1:            // Note the colon, not a semicolon
        playgame();
        break;
      case 2:            // Note the colon, not a semicolon
        loadgame();
        break;
      case 3:            // Note the colon, not a semicolon
        playmultiplayer();
        break;
      case 4:            // Note the colon, not a semicolon
        cout<<"Thank you for playing!\n";
        break;
      default:            // Note the colon, not a semicolon
        cout<<"Error, bad input, quitting\n";
        break;
      }
      cin.get();
    }

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That is because you told the compiler the function should return an int, but you never return anything.
    The original functions with void as return type works because void functions do not return anything; indeed, if you tried to return anything, you would get an error.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    8
    Oh wow, it was that simple

    Thanks! got it fixed by just changing my int too voids.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding memory address using memory pattern
    By MindWorX in forum C++ Programming
    Replies: 1
    Last Post: 05-25-2008, 07:20 AM
  2. My new website
    By joeprogrammer in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 03-17-2006, 07:38 PM
  3. Cprog tutorial: Design Patterns
    By maes in forum C++ Programming
    Replies: 7
    Last Post: 10-11-2004, 01:41 AM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Problem with tutorial (Vector class)
    By OdyTHeBear in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2002, 02:49 PM