Thread: Crucial Question!!!!HELP!!!

  1. #1
    Whats gonna happen now! himanch's Avatar
    Join Date
    Feb 2005
    Location
    Armenia
    Posts
    26

    Crucial Question!!!!HELP!!!

    include <iostream>

    Why do we use mult (int x, int y) 2 times!!One at the begining and the other close to the end fo the script!!!And also why the one at the end does not have semicolon after its paranteses!!!!???
    !!!!Please Help!!!


    Code:
    using namespace std;
    
    int mult( int x, int y);
    
    int main()
    {
        int x;
        int y;
        
        cout<<"Please enter 2 variables x and y\n";
        cin>> x >> y;
        cin.ignore();
        
        cout<<"the mult of the 2 variables is"<< mult (x, y) <<"\n";
        cin.get();
    }
    int mult( int x, int y)
    {
        return x*y;
    }
    Hayda!!!Whats gonna happen tomorrow

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Abuse of ! post of the week award

    > int mult( int x, int y);
    This is a prototype for the function. It tells the compiler how to call the function, without actually telling the compiler what it does.

    > int mult( int x, int y)
    This is the beginning of the definition of the function. Here the compiler finally knows what the function does.

    In large programs, prototypes are put into header files (.h files), and function definitions can be in difference source files (.cpp files).
    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.

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> Abuse of ! post of the week award

    No, all of his posts are like that.

    himanch: Read your mail.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Now now adrianxw there was the one time. Of course maybe they were sick

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    And also why the one at the end does not have semicolon after its paranteses!!!!???
    Because one is a function declaration(or function prototype), and the other is a function definition. A function declaration is just the function header:
    Code:
    int mult( int x, int y)
    {
        return x*y;
    }
    followed by a semicolon, and alerts the compiler not to produce an error because the function definition will follow later.

    You could just put all your function definitions before main(), and avoid most problems like that, but what if you have a function A() that in certain cases calls a function B(), and function B() in turn calls A() in certain cases. If you put the definition of A() first, then the compiler will see a call to B() inside A(), and it will complain that B() isn't defined. The same thing will happen if you put the definition of B() first. Therefore, C++ provides a way around that problem with function declarations. You would do this:
    Code:
    void B(int x, int y);
    void A(int h, int i)
    {
        ...
        if(something == something_else)
            B(1, 2);
    }
    void B(int x, int y)
    {
        ...
        ...
        if(this == that)
            A(10,20);
    }
    Last edited by 7stud; 02-07-2005 at 01:12 PM.

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    465
    Reallllyyy?
    In my calculator program I put it like this

    Code:
    #include<iostream>
    
        int mult(int x, int y)
        {
        return x*y;
        }
    
        int main()
    My computer is awesome.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    you can define them in the begginning too. without having to make a prototype.

  8. #8
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    You will get errors at compile time if you try to call a function before it is defined. If your function definitions are before the first use of the function, all is hunky dory. However, the larger your programs or projects get, the more difficult this becomes to maintain.

    So instead, you place the prototype in the code which basically tells the compiler "I know you haven't seen this function, but trust me it will be defined before the compile is finished."

    If you've ever had any experience with Pascal, you'd realize what an enormous convenience this is.

Popular pages Recent additions subscribe to a feed