The return statement:
Returning from the function is done by using the return statement. The return statement stops execution and returns to the calling function. When a return statement is executed, the function is terminated immediately at that point. The return statement is used to return from a function. It is categorized as a jump statement because it terminates the execution of the function and transfer the control to the called statement.
Syntax:
return expression/variable;
Example :
retum(a + b); retum(a);
return; // to terminate the function
The Returning values:
The functions that return no value is declared as void. The data type of a function is treated as int, if no data type is explicitly mentioned.
For example:
int add (int, int);
add (int, int);
In both prototypes, the return value is int, because by default the return value of a function in C++ is of type int.
Returning Non – integer values: A string can also be returned to a calling statement.
Output:
Example: Function with Non Integer Return Chennai
The Returning by reference:
#include
using namespace std;
int main( )
{
int n 1 = 150;
int &n 1 ref = n1;
cout << “\nThe Value of N1 = “<< n1 << “and n 1 Reference = ”<< n 1 ref; n 1 ref++;
cout << “\n After nl increased the Value of N1 = ”<< n1;
cout << “and n 1 Reference = ”<< n 1 ref;
retum(0);
}
Output:
The Value of N1 = 150 and nl Reference =150 After n1 increased the Value of N1 = 151 and n1 Reference =151