The return statement in C + + is an important statement in a function that ends the currently executing function and returns control to the function that calls the function.
there are two forms of a return statement :
Return
return expression;
1, no return value of the function
A return statement with no returned value can only be used to return a function of type void, which is intended to cause the end of a function to be enforced, similar to the function of a break statement in a looping structure.
Example:
Copy Code code as follows:
void swap (int &v1,int &v2)
{
if (V1==V2)
Return
int temp=v2;
V2=V1;
v1=tmp;
}
Functions that return a type void usually cannot use the second form of a return statement, that is, it can return the result of the invocation of another function that returns the same type of void:
Copy Code code as follows:
void Do_swap (int &v1,int &v2)
{int temp=v2;
V2=V1;
v1=tmp;
}
void swap (int &v1,int &v2)
{
if (V1==V2)
return false;
Return Do_swap (V1,V2)
}
2, function with return value
Any function that returns a type that is not void must return a value, and the type of the return value must be the same as the return type of the function, or it can be implicitly converted to the return type of the function.
Although C + + does not guarantee the correctness of the results, it guarantees that the function returns the appropriate type of result every time return. For example, the following program cannot be compiled by: