How to use return statements in C + + functions _c language

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.