How does the exit () and return () differ

exit () is used to exit the program as a whole. In other words it returns control to the operating system.


After exit () all memory and temporary storage areas are all flushed out and control goes out of program. In contrast the return statement is used to return from a function and return control to the calling function.


Also in a program there can be only one exit () statement but a function can have number of return statements. In other words there is no restriction on the number of return statements that can be present in a function.


exit () statement is placed as the last statement in a program since after this program is totally exited. In contrast return statement can take its presence anywhere in the function. It need not present as the last statement of the function.


It is important to note that whenever a control is passed to a function and when it returns back from the function some value gets returned. Only if one uses a return  statement the correct value would get returned from the called function to the calling function.


For instance consider the program


main ()

{

int a;

int b= 5;

a = f1(b);

printf("a=%d",a);

}

f1(a1)

int a1;

{

int a2;

a2 = 5 * a1;

printf("a2=%d",a2);

return(a2);

}


The value of a2 is calculated in function f1 ( ) is returned to the calling function using return () statement and hence output of program is


a2=10

a=10

Questions by GeekAdmin   answers by GeekAdmin

Showing Answers 1 - 6 of 6 Answers

Have_Phun

  • Feb 3rd, 2007
 

the exit() routine simply terminates the program and returns control to OS (by default)the return statement simply exits the currently executing procedure block and the control goes back to the caller function

  Was this answer useful?  Yes

Anil

  • May 23rd, 2007
 

exit() will free up resources associated with process.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions