Quote:
when we are returning the value as 0 for the main() which is make sure that the given program is executed properly .But can we return a user defined function in main()
You are not returning a user defined value, but a value from a function.
int add(int a,int b)
This function returns int, which is a built-in type in C++. And so you can use these functions in your programs to return the values.
Read the paragraph after following quotation. However, if you wanted to do something like this,
class userdefined {
};
userdefined main() {
userdefined defined;
return defined;
}
This would not fall in the standards of C++ and you will be forcing the program to work in a way that it is not expected to. Compiler will complain about a few things, such as, "main must return int". That is because of the specifications.
Quote:
when we are returning the value as 0 for the main() which is make sure that the given program is executed properly
To be precise, 0 comes from the standards, where as the actual macro for this is "
EXIT_SUCCESS
". This is to tell the underlying framework, that the program terminated with success,
normal termination. When you return another value from the function, underlying framework or the service may not know that the result was intended. Framework would consider it to be some sort of error or what-ever that framework or operating system has in the database. That is why, programs must return 0, if you feel unhappy with this you can skip this line as of C 1999 because the compiler itself will add the
return 0;
at the end of the program, directing framework to consider it to be a normal termination.
For more please read the following documents,
Main function - cppreference.com[
^]
Structure of a program - C++ Tutorials[
^]
c++ - Should I return EXIT_SUCCESS or 0 from main()? - Stack Overflow[
^]
No standard for main function:
That is wrong, there is a standard for this and compilers follow that. For a demo,
try this[
^]:
class defined { };
defined main()
{
defined usrdfnd;
return usrdfnd;
}
You can read the error message and see what it says. You can however, always change the compiler procedures and modify the way it follows the standards.