Chapter 7 Functions

Source: Internet
Author: User

7.1 Function Definition

1. Call Operator)

2. The function body is also a scope.

3. Form parameters and real parameters

7.1.1

A function cannot return another function or a built-in array, but it can return a pointer to a function or to an array element.

C ++ is a static, strong-type language.

7.2.1 non-reference parameters

Pointer Parameter

Const Parameter

7.2.2 reference parameters

1. Use reference parameters to return additional information

2. Avoid assignment using const reference

If the only purpose of using a reference parameter is to avoid parameter assignment, you should use the const Application

3. More flexible const references

4. Pass the reference pointing to the pointer

7.2.3 vector and other container-type parameters

7.2.4 array parameters

Definition of array parameters

Void printvalues (int *)

Void printvalues (INT [])

Void printvalues (INT [10])

The three definitions are actually the price types are int *

Array parameters

Pass an array by referencing

It is mostly transmitted as an array parameter.

Multi-dimensional arrays are actually arrays of arrays.

Therefore, when passing multi-dimensional array parameters

Void printvalues (INT (* matrix) [10])

Note that int (* matrix) [10] refers to a pointer to an int array with ten elements.

Int * matrix [10] refers to an array of ten pointer types.

Note that the compiler ignores the array length in the form parameter.

7.2.5 processing of the array passed to the Function

The type check of Non-referenced array parameters only ensures that the real parameters and array elements have the same type pointer, so it is best to explicitly pass the length of the array

7.2.6 main command line processing options

Int main (INT argc, char ** argv) int argc is the number of parameters. argc only has pointers to argc char * arrays.

7.2.7 functions with variable parameters

When you cannot list all types and numbers of real parameters passed to the function, you can use the omitted parameter,

7.3 Return Statement

7.3.1 functions without return values

Note that the void function can also have a return statement, which can end directly after return or return a function of the same void type.

7.3.2 function with return value

1. A function whose return value is not void must return a value, unless this function is a main function, because the compiler will implicitly insert a statement that returns 0.

2. Return non-reference type

3. Return reference

Do not return references to local objects

5. return values can be referenced to return the left value.

Get_val (S, 0) = 'a ';

Do not return a pointer to a local object

7.3.3 Recursion

Directly or indirectly call your own function to become a recursive function)

Note that the main function cannot call itself.

7.4 function declaration

A function declaration consists of the return type, name, and parameter list of the function. These three elements are called function prototype)

Default real parameters

The real parameters of a function call are parsed by location. By default, the real parameters can only be used to replace the missing tail parameters of the function call.

The default real parameter can be any appropriate type expression.

7.5 static local variables

In C ++, each name has a scope, and each object has a life cycle.

7.5.1 Automatic Object

Only when the function is called, the objects in the dish are called automatic objects.

7.5.2 static local variables

If a variable is in the scope of a function, but is angry with it, it is called multiple times, then this object should be defined as static

7.6 inline functions

Inline functions can avoid the overhead of function calls. Inline functions are just a suggestion. The compiler can ignore this suggestion. Note that inline functions are defined in the header file.

Member functions of the 7.7 class

7.7.1 define the function body of a member function

1. Invisible Parameters

2. Each member function has an invisible parameter This

3. Constant member functions

A function modified with const is called a const member function. Such a function cannot modify the object of a function.

7.7.2 use the scope operator to define member functions outside the class

7.7.3 Constructor

Constructor is a special member function. constructor and class have the same name and no return value.

Define constructors, which are generally defined in public.

Note the initialization list of the constructor.

The code between the colon and curly braces is called the initialization list of the constructor. One or more data members of the constructor's initialization list specify the initial values.

If no explicit constructor is defined for a class, the compiler will automatically generate default constructor for this class. This function is called the default constructor for synthesis.

7.8 overload Functions

Two functions appear in the same scope. If they have the same name but different form parameters, they are called overload functions.

7.8.1 overload and scope

For functions in different scopes, the sub-scopes will cover the implementation of functions in the parent scopes.

7.8.2 function matching and real parameter conversion

7.8.3 three steps for determining the heavy load

7.8. Real parameter type conversion

1. Exact match

2. Type Improvement

3. Pass standard conversion

4. class type conversion

7.9 pointer to function

The pointer type is irrelevant to the function name.

1. Use typedef to simplify function pointer Definition

2. pointer to function and initialization

Function pointers can only be initialized or assigned values through functions of the same type, function pointers, or constant expressions of 0 values.

3. Call a function through a pointer

4. function pointer Parameters

Void fun (bool (int A, int B), int)

Or

Void fun (bool (*) (int A, int B), int)

5. Return the pointer to the function.

INT (* ff (INT) (int *, INT );

FF is a function with an int parameter. The function returns a function pointer of INT (*) (int *, INT, using typedef is a simple and easy-to-understand function.

Note that the parameter can be defined as a function type, but the return value type of the function must be a pointer to the function, not a function.

6. pointer to the overloaded function

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.