what is meant by return and void??

what is the function of return?
can change the return 0 into return 1 /2/3 or other integer?

for void, what is the function? if i use void ,then needed to use return 0?

Return is a keyword used to return a value from a function. Void functions do not have return types. If you, for example, have a function like "int getNumber()", then the return type is int. Here is an example of using return.

1
2
3
4
5
6
7
8
9
10
11
12
int getNumber();

int main(int argc, char** argv) {
    int num = getNumber(); // Will return 5. The variable num will now be equal to 5.
    // double otherNum = getNumber(); This causes an error, because you cannot explicitly convert from int to double.

    return 0;
}

int getNumber() {
    return 5;
}
A void as a function return type means don't return anything.
I believe he means, what's the different between int main() and void main() and what's the point of return 0; at the end of your main function.

For the main function, int main() is the only standard way. Void main() might compile on some compilers, but that's just Microsoft making exceptions. Don't ever use void main(), it's evil.

And for the returning, it has to do with the OS you run your code on. Returning 0 at the end simply means that the program ended successfully. When the OS finds that 0 is returned, it knows that the program is done and will close it properly. Not returning 0 usually isn't a problem, but it's standard, so just do it anyways.
As for having other outputs, yes, it is possible. This is used to indicate that the main function has encountered some error. Any non-zero integer output means the program ended with an error (and the OS will act accordingly).
Last edited on
Topic archived. No new replies allowed.