Thread: A question...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    15

    Question A question...

    I got a question...
    When do i need to use return 0 ?
    I am a newbie,thanks for responding!
    S.b

  2. #2
    Registered User Casey's Avatar
    Join Date
    Jun 2003
    Posts
    47
    >When do i need to use return 0 ?
    When you need to return 0. You can pass 0 as a null pointer, or the integer zero to mean whatever you'd like.

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

    int main() returns an integer

    The int in int main() means that the main() function should return an integer value (sends a zero to the operating system).

    Main should return zero by default. A number other than zero usually tells the operating system that the program was "abnormally terminated".

    If a function has a return type of void i.e void MyFunction() then it doesn't return anything.

    In any non-void function the return value depends on what the function is doing. If its an addition function, the return value will be the sum.

    [EDIT]
    Next time, please use a title for your post that indicates what the question is about. See the board guidlines & hints.
    Last edited by DougDbug; 06-27-2003 at 10:36 AM.

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    15
    so u mean void is 'return' ?
    and i know 'public' is open to the whole program but wat about 'private'?
    S.b

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    15
    i can't understand about the classes...


    void Aclass::aFunction()
    {
    cout<<"Whatever code";
    }
    #include <iostream.h>
    class Computer //Standard way of defining the class
    {
    public:
    //This means that all of the functions below this(and any variables)
    //are accessible to the rest of the program.
    //NOTE: That is a colon, NOT a semicolon...
    Computer();
    //Constructor
    ~Computer();
    //Destructor
    void setspeed(int p);
    int readspeed();
    //These functions will be defined outside of the class
    protected:
    //This means that all the variables under this, until a new type of
    //restriction is placed, will only be accessible to other functions in the
    //class. NOTE: That is a colon, NOT a semicolon...
    int processorspeed;
    };
    //Do Not forget the trailing semi-colon
    Computer::Computer()
    { //Constructors can accept arguments, but this one does not
    processorspeed = 0;
    //Initializes it to zero
    }
    Computer::~Computer()
    { //Destructors do not accept arguments
    }
    //The destructor does not need to do anything.
    void Computer::setspeed(int p)
    { //To define a function outside put the name of the function
    //after the return type and then two colons, and then the name
    //of the function.
    processorspeed = p;
    }
    int Computer::readspeed()
    { //The two colons simply tell the compiler that the function is part
    //of the clas
    return processorspeed;
    }
    int main()
    {
    Computer compute;
    //To create an 'instance' of the class, simply treat it like you would
    //a structure. (An instance is simply when you create an actual object
    //from the class, as opposed to having the definition of the class)
    compute.setspeed(100);
    //To call functions in the class, you put the name of the instance,
    //a period, and then the function name.
    cout<<compute.readspeed();
    //See above note.
    return 0;
    }


    wat does the ' :: 'mean?
    S.b

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    The :: operator is the scope resolution operator. It's used to define that the right side of it is part of the left part. Look at the definition for the class computer. It defines functions but they don't have any code for them. After the definition, the coder redefines the functions and adds the Computer:: part to show that that function is the one described in the Computer class. Straight forward, eh?

  7. #7
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    A couple other things about that code:

    Use code tags... please!

    <iostream.h> is not a standard header, <iostream> with namespace std is.

    Also, I'm guessing there is more to the file than posted because of the function 'aFunction' belonging to a class which is otherwise unreferenced, but typically, all function/class declarations and definitions are placed below the includes. If they aren't, then yuo can get odd errors along the line of 'undefined symbol cout' in the functions above them.

    >> so u mean void is 'return' ?

    Right, functions with a void return value return nothing, and to explicitly return from them, you use 'return;'.


    >> and i know 'public' is open to the whole program but wat about 'private'?

    There is actaully a third one as well: protected, which is somewhere in between the two.

    public, as you said allows the any part of the program to see those parts of the class.

    private only allows the class itself and functions/classes declared to be friends to have access. (Not even derived classes will have access.)

    protected, in addition to allow friends access to that part of the code, also let derived classes have access to it.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by Casey
    >When do i need to use return 0 ?
    When you need to return 0. You can pass 0 as a null pointer, or the integer zero to mean whatever you'd like.
    NULL is NOT guaranteed to be zero. NULL is allowed to be platform-specific. On many popular operating systems, it is zero, but nothing mandates this. On other types of systems, NULL is nonzero.

    Bottom line, if you're returning a null pointer, use NULL and not 0.

  9. #9
    Registered User Casey's Avatar
    Join Date
    Jun 2003
    Posts
    47
    >>NULL is NOT guaranteed to be zero.
    That's true, but to be more accurate, NULL is not guaranteed to be represented by all bits zero.

    >>NULL is allowed to be platform-specific.
    Also true.

    >>Bottom line, if you're returning a null pointer, use NULL and not 0.
    This is not true. The C++ standard says that an integral constant that evaluates to zero and used in a pointer context is a null pointer.
    A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to
    zero. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that
    type and is distinguishable from every other value of pointer to object or pointer to function type.
    In that case, 0 and NULL are the same. Stroustrup 5.1.1 pp.88 says that 0 causes fewer problems, so I tend to use 0.

  10. #10
    Registered User
    Join Date
    Jun 2003
    Posts
    15
    thanks guys
    S.b

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM