I got a question...
When do i need to use return 0 ?
I am a newbie,thanks for responding!
I got a question...
When do i need to use return 0 ?
I am a newbie,thanks for responding!
S.b
>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.
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.
so u mean void is 'return' ?
and i know 'public' is open to the whole program but wat about 'private'?
S.b
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
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?
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.
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.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.
Bottom line, if you're returning a null pointer, use NULL and not 0.
>>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.
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.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.
thanks guys
S.b