return Statement:

Before studying the difference let’s start with return statement functionality. In any programming language return statement cause execution to leave the function and return to the line from where that function is called. Return statement in any function is consider as the last statement because whatever written after return statement will ignore by the compiler and it’ll move back from where that function is called.

Return statement also return the value to the caller function. For example, a function is written and its job is to add two numbers and you need that result in other function you can simply use return statement to return the result to that function. Type of value return by return statement depends on return type of function. If function return type is int than it can only return integer value. Similarly, if the function is of void return type then return statement can be return as return;. Remember that unction reaches at the end of the function without encountering return statement by default return;will be executed.

Difference B/W return 0, return 1 and exit(0):

Well now after learning the functionality of return statement let us focus on “Why we use return statement in main() function?” or “Where does the returned value from main() function go?”

Well when you compile a program it starts from main() function. After reaching at the end of the main() function it then calls exit() function (which you can call in your program as well). This function then again call _exit() function which tell the OS to save the value returned from your main() function in the process table (where information about your process is saved). Your return value is given to the calling process and your process is then removed from the process table.

Now return 0; in main function means that program succeeded and ends with no error. Whereas on the other hand return 1; or you can say return n; (where n can be any number) means that program ends with an error. The only purpose of returning number is that it’s helps in debugging.

Now move to the function exit(n). Well as discussed above exit(n) function is used to exit from the program. Similar to return statement exit(0) means program ends with no errors. And exit(n) means program ends with error. The difference between exit and return is that return in main only cause the program to exit whereas exit() function can be called in any function in program and it’ll terminated the program from there.