return type and return statement: No return value function, return value function, return array pointer

Source: Internet
Author: User
Tags exit in


1.return statement function: Terminates the currently executing function and returns control to the place where the function was called. Return statement in two forms:


Return expression;

2. A function that returns void does not require a return statement because the return is implicitly executed after the last sentence of such a function .

3.Void function If you want to exit in the middle, you can use return, the equivalent of break, early exit.

4. A function with a return type of void can also use the return expression form, but The return statement's Expression must be another function that returns void.

The return value type of the 5.Return statement must be the same as the return type of the function, or it can be implicitly converted to the return type of the function.

6. There should also be a return statement after the loop that contains the return statement , and if not, the program should be wrong.

7. Once the function is complete, the storage space It occupies is also released. Therefore, a function termination will mean that a reference to a local variable will point to a region of memory that is no longer valid.

Tip: To make sure the return value is safe, we might ask: which object does the reference refer to that already existed before the function?

8. It is wrong to return a reference to a local object, again, a pointer to a local object is also wrong. Once the function is complete, the local object is disposed, and the pointer points to an object that does not exist.

9. The reference returns the left value! appears to the left of an assignment statement and cannot be assigned a value if the return type is a constant reference.

10.C++11 New standard specifies that a function can return a list of values enclosed in curly braces.

Return {};

Return {"Functionx", "Okey"};

Return {"Functionx", expected, actual};

If the function returns a built-in type, the curly brace-enclosed list contains a maximum of one value.

11.C++11 New Standard Rules: We allow the main function to end without a return statement, because if the main There is no return statement at the end of the function , and the compiler implicitly inserts a return statement that returns 0 .

12. The preprocessing variable cannot be preceded by the std::, nor can it appear in the using declaration.

13. Recursive function: If the function calls itself, whether the call is direct or indirect, it is called a recursive function.

Note:themain function can not call itself!

14. The function cannot return an array because the array cannot be copied. However, you can return a pointer or reference to an array.

15. The function of returning an array pointer is as follows:

Type (*function (parameter_list) [dimension]

Type: represents the types of the elements,dimension represents the size of the array, andthe parentheses at each end(*function (parameter_list)) must exist.

TIP: If you do not have this pair of parentheses, the return type of the function will be an array of pointers.

return type and return statement: No return value function, return value function, return array pointer

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: 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.