Return vs exit in C++ by R4R Team

Return statement vs exit statement:
-return not close the entire program while exit() close the program.
-return statement may return some value from the function to the function calling but exit() not return any value.
-return value also depend on the return type of the function while exit() work anywhere in the program.

Return program:

#include < iostream>
using namespace std;
int sqaure(int n)
{
return n*n;
cout<<"This is after return";
}
int main()
{
int n,a;
cout<<"Enter number";
cin>>n;
a=sqaure(n);
cout<<"sqaure is: "<< a;
return 0;
}


output-

Enter number
3
sqaure is: 9

exit() program:

#include < iostream>
using namespace std;
int main()
{
cout<<"First linen";
exit(0);
cout<<"Second line";
return 0;
}


output-

First line




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!