C ++ primer plus Study Notes (6)

Source: Internet
Author: User

1. define functions

1. c ++ has certain restrictions on return values: it cannot be an array, but it can be any other type-integer, floating point, pointer, it can even be a structure and an object (although an array cannot be directly returned, an array is returned as a structure or part of an object ).

2. How do functions return values?

The function returns the value to the specified CPU register or memory unit. Then, the calling program will view the memory unit. The data type stored in the memory unit is known through the definition of the function prototype.


2. Functions of the function prototype:

1. the compiler correctly processes the function return value.

2. the compiler checks whether the number of parameters used is correct.

3. If not, convert it to the correct type (if possible ).

Prototype during compilation is called static type check ). It can capture many errors that are very difficult to capture during the running stage.


3. Pass function parameters and values

1. form parameter (parameter): the variable that receives the passed value; real parameter (parameter): the value that is passed to the function.

2. the variables declared in the function (including parameters) are private to the function and are automatically released after the call ends.

It is called local variables and automatic variables.


4. Functions and Arrays

Advantages and disadvantages of using arrays as parameters:

1. Save the time and memory required to copy the entire array.

2. increased the risk of damage to raw data.

You can use the const qualifier to solve this problem. -- Void show (const int arr [], int N); this does not mean that the original array must be a constant, but does not mean that arr cannot be used in show () to modify data. This disables arr [0] + = 10.

5. pointer and const

1. the pointer points to a constant object, which prevents the pointer from being used to modify the value.

Const int * PT = & num;

The declaration of Pt does not mean that the value it points to is actually a constant, but for PT, this amount is a constant. You cannot modify the value of num through PT, but you can change the point of the PT pointer.

Remember: if the data type itself is not a pointer, you can assign the addresses of const data or non-const data to the pointer to const, however, only the addresses of the const data can be assigned to non-const pointers.

2. Declare the pointer as a constant to avoid changing the position pointed to by the pointer.

Int * const Pt = & num; you can modify the value of num through PT.

3. Two methods cannot be modified:

Const int * cont Pt = & num;


6. functions and structures

Similar to the behavior of a Single-value variable, the structure can be passed by value. disadvantage: If the structure is very large, the replication structure will increase the memory requirement and reduce the system running speed. ---- You can pass the structure address to solve the problem.

Void show (const polar * PDA)

{Cout <PDA-> distance;

}


7. function pointer

1. The function also has an address. The function address is the memory start address for storing the machine language code.

2. The address of the function or the return value of the function must be differentiated.

1. Process (think); enables process () to call the think () function internally;

2. Thought (think (); First Call think (), and then pass the return value of think () to the thought () function.

3. Declare a function pointer

1. Double (* PT) (INT); (* PT) is a function, and PF is a pointer to the function.

2. Double * PT (INT); Because () has a higher priority than *, all expressions assume that PF () is a function that returns pointers. (Wrong !)

3. Example of calling a function pointer:

Double Betsy (INT );

Double PAM (INT );

Void estimate (INT lines, double (* PF) (INT ));

You can perform the following operations: 1. Estimate (line, Betsy); 2. Estimate (line, Pam );

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.