Suppose that a program is in the middle of some awkward process in a
function which is not main(), perhaps two or three loops working together,
for example, and suddenly the function finds its answer. This is where the
beauty of the return statement becomes clear. The program can simply call
return(value) anywhere in the function and control will jump out of any
number of loops or whatever and pass the value back to the calling statement
without having to finish the function up to the closing brace }.
myfunction (a,b) /* breaking out of functions early */

int a,b;
{
while (a < b)
{
if (a > b)
{
return (b);
}
a = a + 1;
}
}



The example shows this. The function is entered with some values for a and
b and, assuming that a is less than b, it starts to execute one of C’s loops
called while. In that loop, is a single if statement and a statement which
increases a by one on each loop. If a becomes bigger than b at any point
the return(b) statement gets executed and the function myfunction quits,
without having to arrive at the end brace ‘}’, and passes the value of b back
to the place it was called.
Designed By Blogger Templates | Templatelib & Distributed By Blogspot Templates