
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Return Statement vs Exit in Main Using C++
If you are a programmer, you write the code; If you write the code, you use the function; if you use the function, you use return and exit statements in every function. So In this article, we will discuss what a return statement and exit statement are and their differences.
In C++,
- return is a statement that returns the control of the flow of execution to the function which is calling.
- Exit statement terminates the program at the point it is used.
int main()
This is where the execution of the program begins. The program is executed from the main() function, and since we have int in place of the return type, it has to return some integer value. We can say this integer represents the program's status, where 0 means the program executes successfully. A non-zero value means there is an execution error in the code.
Return statement in main() function
A return statement always returns the control of flow to the function which is calling. Return uses exit code which is int value, to return to the calling function. Using the return statement in the main function means exiting the program with a status code; for example, return 0 means returning status code 0 to the operating system. Let us look at a C++ program using the return statement.
Example
#include <iostream> using namespace std; class Test { public: //To activate Constructor Test() { cout<<"Hey this is Return Constructor \n"; } //To activate Destructor ~Test() { cout<<"Hey this is return Destructor"; } }; int main() { //Creating object of Test class Test object; //Using return in main return 0; }
Output
Hey this is Return Constructor Hey this is return Destructor
Looking at the above program, we can say that return calls the constructor and destructor of a class object. Calling the destructor is essential to release the allocated memory.
Exit() statement in main function
Exit () statement terminates the program at the point it is used. When the exit keyword is used in the main function, it will exit the program without calling the destructor for locally scoped objects. Any created object will not be destroyed and will not release memory; it will just terminate the program.
Example
#include <iostream> using namespace std; class Test { public: //To activate Constructor Test() { cout<<"Hey this is exit Constructor \n"; } //To activate Destructor ~Test() { cout<<"Hey this is exit Destructor"; } }; int main() { //Creating object of Test class Test object; //Using exit() in main exit(0); }
Output
Hey this is exit Constructor
Looking at the program's output, we can conclude that using the exit keyword in our program means it will not call a destructor to destroy allocated memory and the locally scoped objects.
Conclusion
In this article, we understand the differences between return statements and exit statements. We can conclude from the differences that using them makes a lot of difference in our program. Using exit over return requires precautions. However, they don't impact much on one page of code and impact a lot when developing software. We hope you find this article helpful.