exit and return statements

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 be presented as the last statement of the function.

It is important to note that whenever a control is passed to a function and returns back, 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 () and is returned to the calling function using return () statement and hence output of program is

a2=10
a=10

 

Editorial Team at Geekinterview is a team of HR and Career Advice members led by Chandra Vennapoosa.

Editorial Team – who has written posts on Online Learning.


Pin It