C ++ learning 8th-deep Functions

Source: Internet
Author: User

1. function parameters and real parameters

1) form parameters and real parameters

Normally, function parameters (parameters) and function independent variables (real parameters) are interconnected.

Function parameters-variables in the function prototype and function declaration:


Function arguments-when a function is called, input a variable that replaces the function parameters;


Note: The parameters of a function only have the range of local variables. The function call ends and the parameters are naturally destroyed;

Three parameter passing Methods: Value passing, reference passing, and address passing

2.Value Transfer

Value Transfer-assign a value to the function for execution;


Because only a copy of the real parameter is passed, the function cannot modify the value of the original real parameter;


Advantages of value transfer:

A) variables, values, and expressions can be passed;

B) The parameter will not be modified;

Disadvantage: it takes a long time to pass a struct or class;

In most cases, value passing is the best function parameter passing method, which is flexible and secure.

3. Transfer references

The return value transmitted by the value can only be implemented through the return function;

In most cases, it is appropriate to pass values. However, it is valid and clear to modify the values of parameters, for example, to rearrange an array;

You can modify the value of a parameter after passing a reference;


Another example is:


A function generally has only one return value. To return multiple values, you only need to pass multiple references:


2) constant reference Transfer

It is time-consuming to pass references without passing values to struct or class objects. References are used to create real arguments without copying any information;

When the parameter is being referenced, the function can modify the parameter value. If the parameter value cannot be modified, it can be passed through constant reference:


The above example shows a compilation error!

Several reasons for passing using constant reference:

A) assist the compiler to ensure that parameters that cannot be changed will not be modified; B) notify the programmer to check whether the parameters will be modified; c) Help the programmer debug the error value.

Remember: constant reference is usually used for transmission, unless you really need to change the value of the parameter.

Advantages of reference transfer:

A) Allow the function to modify the parameter value, which is sometimes very useful; B) reference does not involve copying parameter information, so it is faster;

C) constant references can be passed to prevent unintentional modifications. d) multiple values can be returned.

Disadvantages:

A) constant reference cannot be a value or expression, but a variable. B) It is difficult to clearly indicate whether the referenced data is input, output, or both;

C) The function call cannot be used to determine whether the parameter is modified;

The transfer rate of reference is slow!

4. Address Transfer

Address Transfer is another way of transferring variables. Only the address of the variable is passed, instead of the variable itself. The function parameter must be a pointer.


Typical application of Address Transfer dynamically allocates memory and accesses Arrays:


In the preceding example, the length of the array must be input.

Generally, it is better to take a null pointer to the parameter passed by the address before resolving the reference:


Advantages of address transfer:

A) Allow the function to change the parameter value; B) because the copy is not performed, the speed is faster; c) multiple values can be returned.

Disadvantage: a) the parameter cannot be a value or expression and must be a normal variable. c) All parameters must be checked for null pointers. d) It is slower than passing values.

It is generally safer to transfer a reference than to transmit an address.

2) there is no major difference between value transfer, reference transfer, and address transfer:

A) Reference transfer, which is only processed as a pointer during compiler processing;

B) the main difference between pointer and reference transfer is that there are clear and strict syntaxes for reference transfer. Therefore, reference transfer is safer, but not more flexible. However, it is equally efficient.

D) address transfer only transmits the address value of the address. If the function changes the address value, it only changes the temporary value and does not change the actual value of the address.


Although address transfer is actually a value transfer, you can still modify the stored value of the transfer address. In this case, the differences between reference transfer, address transfer and value transfer are as follows.

To modify the actual address of a parameter through address transfer, you can pass the address reference:


The conclusion is that the reference is a pointer, And the pointer address is a value transfer.

5. The returned values are values, references, and addresses.

The value, reference, and address returned to the function call are almost the same.

1) return value is the simplest and safest way:

The returned variables or expressions can contain the local variables declared by the function;

Structures and class objects are relatively slow;

2) return a reference

The return value of a reference must be a variable. The caller can continue to use this reference to modify the variable. This is often useful and fast;

Note that you cannot return a reference to a local definition of the function:


3) return address

The address returned can only return the address of the variable, not the value and expression;

Address return is also relatively fast, and the address of local variables cannot be returned;

If the returned address is the address of the local variable that calls the function, the compiler generates a warning message;

One common address is to return a new allocated memory to the caller:

Summary:

Generally, the return value is sufficient, flexible, and secure. It is useful to return a reference and an address, especially for the Dynamic Address Allocation of struct and class objects;

Make sure that the return is not a reference or address of the local variable customized by the function.

6. inline functions

Functions can be used in a few ways: function code can be reused; code can be changed and updated easily; code can be read and written more easily; type check is provided;

More operations are often required for each function call; local code is better;

In-Place encoding means that when a function is called, the code is directly embedded in the current position. This is a so-called inline function, using the keyword inline

Inline functions are suitable for short functions, especially in loops without branches.

7. Function Overloading

Function Overloading is a major feature of C ++. Different functions can be created with the same name and different parameters;


Note that the return type of a function cannot be used as a flag for function overloading. Using typedef cannot be understood as creating a new type;

Possible 3 situations of matching overload functions:

1) find the matching function; 2) No matching function is found; 3) Multiple overloaded functions meet the condition (Fuzzy Matching ).

Reload function matching process:

1) find the most appropriate one; 2) Matching through reasonable conversion (parameter upgrade conversion, such as Char-> INT );

3) Use a forced standard conversion (such as int-> double); 4) when none of the above are found, C ++ will traverse user-defined functions;

Ambiguity matching is a compilation and running error.

Process ambiguity matching:

1) define a new overload function;

2) explicitly convert ambiguous parameters to a specific type;

Conclusion: function calling significantly reduces the flexibility of functions with a little risk. Function overloading may be complicated but very practical.

8. Default Parameters

The default parameter, that is, the parameter of the function has been assigned an initial value.


The default parameter is an excellent choice. You do not need to modify the default value required by the function;

Note: The value assignment cannot be skipped because there are more than one default value;

A) All default parameters must be from right to left; B) the rightmost default value is the most often overwritten and modified; c) the default value cannot be used as a flag for function overloading.

9. function pointer

Function pointers are advanced topics. For array names, function names can be understood as function addresses;

When a function is called, the function name (that is, the function address) is unreferenced and the function address entry is obtained before execution starts;

The function pointer is the pointer to the address of the function, and the function pointer can be redirected:

Because () has a higher priority than *, the function pointer must be enclosed in brackets:


Note: The signature of the function pointer (including parameters and return values) must match the signature of the function to which the pointer belongs.

Function pointer calls, just like implicit calls to a function, such as pfoo ();

Function pointers are usually used to implement specific functions. However, you can customize the functions;

For example, select sorting:


10. Stack and stack

The memory used by the program is divided into four parts:

A) code area: where the compiled program is stored; B) Global Area: where the global variables are stored; c) Heap: where the dynamically allocated variables are stored; d) STACK: the place where the parameters and local variables are stored.

1) Heap-free storage zone is a huge buffer pool dynamically allocated;


It must be known that sequential storage requests do not necessarily obtain the actual sequential storage region;

When the dynamically allocated memory is released, the heap is returned as the new memory that can be allocated;

2) STACK: operations that can only be taken out and put from the top:

Top (), pop (), and push () are common operations;

Stack is the first in, last out (FILO) structure;

When the storage area of the stack is full, overflow occurs.

It is not necessary to have a detailed understanding of stack details.

11. Recursion

The recursion of C ++ is the function's own call.

Recursion must have a termination condition. Otherwise, it is infinite recursion. For example:


For example, calculate the sum of 1 to nvalue:


Example 3: Fibonacci count:



12. namespace

The concept of namespace is definitely not classified as a function, but a very important concept.

The identifier (variable or function name) in the program is in the same range, that is, Name Conflict.

In this case, the namespace can be used to call the required functions or variables through the Global: operator;

You can also use the using keyword to introduce the namespace.

13. capture errors

Errors include assertions, errors, exits, and exceptions.

In programming, you cannot avoid errors, including syntax and Word Meaning errors;

Syntax Error-the program is not programmed according to the syntax format of the corresponding language;

Incorrect meaning-the program does not achieve the goal as the programmer wishes; it cannot be captured by the compiler,

A) logical errors; B) false assumptions.

Processing:

A) Defense errors; B) detection errors;

2) assert

If the assertion condition is true, the program cannot process it. If the assertion condition is false, the program sends an error exception message and terminates the program;


So far, C ++ has finished learning process-oriented programming, followed by C ++ object-oriented programming. Have fun !!!!!!!

[Disclaimer:
1) This content may come from the Internet, or I have sorted it out by myself. It only represents the opinions and opinions of the Internet and individuals!
2) This content is for reference only. Any reference to this content will cause any consequences and will be irrelevant to the original author and the author of this blog !]

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.