Return Statement:
A keyword return is used to terminate function and return a value to its caller. The return statement might also be used to exit a function without returning a value. The return statement might or might not involve an expression.
The common form of the return statement is.
return;
return ( expression);
The return can appear anywhere inside the function body. A function could also have more than one return while it is good programming style for a function to have a one entrance and one exit.
Example:
return;
return (45);
return ( x + y);
return (++j);
return 0;
return i++
A return declaration ends the execution of the function and pass on the control back to the calling environment.
Example:
1. int sum (int a, int b)
{
return(a+b); // return integer value.
}
float maximum (float a, float b)
{
if (a>b)
return a;
else
return b;
}// return floating point value.
Sample program:
// by using several return statements in a function
// source program name: funct2.cpp
# include<iostream.h>
void main (void)
{
float maximum( float, float, float);
float x,y,z,max;
cout<<" enter three numbers \n";
cin>> x >> y >> z;
max = maximum(x,y,z);
cout << "maximum " << max;
}
float maximum (float a, float b, float c)
{
if ( a>b)
{
if (a>c)
return (a);
else
return (c);
}
else
{
if ( b>c)
return ( b);
else
return ( c);
}
}// end of function
output of the program is
enter three numbers
4 5 6
maximum = 6