Return statement
è Some times the called
function may return a value to its calling function. For that return statement
is used.
è It is possible to pass
any number of value from calling function to called function , but the called
function can return only one value.
è The format of return
statement is as follow:
return; or return(Expression);
Here, In first form of
the return statement , it does not return any value. It returns only control to
the caller. When return type of the function is void and want to return control
from the middle of the function, this form is useful.
Ex,
void fun()
{
.....
.....
if(....)
return;
.....
}
In the second form of
return with expression returns the value of the expression
ex,
int fun(int x,int y)
{
.....
.....
return(x+y);
}
A function can have
more than one return statement. Function returns control back to the caller as
soon as first return executes. If return type is not void , return will not
only returns control , but also returns value.
ex,
int diff(int x,int y)
{
if(x>y)
return(x-y);
else if(x<y)
return(y-x);
else
return(x+y);
}
Posted By : Ruchita Pandya
No comments:
Post a Comment