There are two different scenario return statement is used inside c++ programming. We can use return 0 c++ inside main() function or other user defined functions also. But both have different meanings.
- return 0 c++ used inside Main function
- return 0 c++ used inside other user defined function
What is meaning of return 0 and return 1 in c++ ?
There will be two scenario for it. Possible function can be main() function or any other user defined function.
return 0 c++ in main() function.
If c++ program is completed successfully then it will return 0 in main function.
#include <iostream>
using namespace std;
int main() {
// Successfully completing program
return 0;
}
return 1 c++ in main() function.
If there is some error in program or exited with error then it will return 1 in main function.
#include <iostream>
using namespace std;
int main() {
// Errors in completing program (Exited with Error)
return 1;
}
return 0 c++ in boolean return type function
If there is function in c++ with return type boolean then if it return 0 then it means return false. You can use this return for checking that required condition or logic is satisfied or not.
bool func(int iVal) {
if (iVal == 2) {
// Same as return true;
return 1;
}
else {
// Same as return false;
return 0;
}
}
In given function if function return 1 means value is 2 otherwise it will return 0. So we can identify by this function that given value is 2 or not.
return 1 c++ in boolean return type function
If function return 1 means our condition is satisfy and it will return true.
#include <iostream>
using namespace std;
int main() {
// result1 is true (return 1)
bool result1 = func(2);
// result2 is false (return 0)
bool result2 = func(2);
// Successfully completing program
return 0;
}
bool func(int iVal) {
if (iVal == 2) {
return 1;
// Same as return true;
}
else {
return 0;
// Same as return false;
}
}
CONCLUSION:
If we want to exit our program successful then we return 0 inside main function. If there is some error during exiting program then we can return 1 inside main function. In case of other user defined functions which return boolean value. Then we can check conditioning of function using return value of function. Here if function return 1 means condition is TRUE and if function return 0 then conditions is FALSE.
Leave a Reply