C + + Primer Fourth Edition reading notes (vi) function

Source: Internet
Author: User

inline functions, class member functions, and overloaded functions, and function pointers.

Functions can be thought of as programmer-defined operations. As with the built-in operators, each function implements a series of calculations, and then (most of the time) generates a settlement result. But unlike the operator, the function has its own function name, and there is no limit to the number of operands. As with operators, functions can be overloaded, which means that the same function name can be used to respond to a different function.

Definition of a function

A function is uniquely represented by a function name and a set of operand types. The operand of the function, also known as a formal parameter (parameter), is declared in a pair of parentheses, separated by commas between the shape's participating parameters. The calculation of the function execution is defined in a block statement called the function Body (functionbody). Each function has a return type associated with it.

1.1 Invocation of functions

The C + + language implements the invocation of a function using the call operator, which is a pair of parentheses. The operand of the calling operator is a function name and a set of (possibly empty) arguments separated by commas. The result type of a function call is the type of the function return value, and the result of the operation is itself the return value of the function.

A function call does two things: Initialize the formal parameters of the function with the corresponding arguments, and transfer the control to the called function, the execution of the primary function is suspended, and the function is started to execute. The function's run is defined and initialized with formal parameters (implicit).

1.2 function body is a scope

The function body is a block of statements that defines the specific operation of the function. Typically, this block statement is contained within a pair of curly braces, forming a new scope. As with other block statements, you can define scalars in the body of a function. Variables defined in the body of a function are accessible only within that function. This variable is called a local variable, and they are "local" relative to the function that defines them, whose name can only be seen in the scope of the function. This variable exists only when the function is running.

1.3 Formal parameters and arguments

Similar to local variables, the function's formal parameters provide a named local storage space for the function. The difference between them is that formal parameters are defined in the formal parameter list of a function and are initialized by the arguments passed to the function when the function is called.

An argument is an expression. It can be a scalar or literal constant value, or even an expression that contains one or several operators. When a function is called, the number of arguments passed must be exactly the same as the number of parameters of the function. The type of the initialization must match the type of the object being initialized, and the type of the argument must exactly match the type of the parameter it corresponds to: the argument must have a data type that is the same as the formal parameter type, or that can be implicitly converted to a formal parameter type.

1.4 Function return type

The return type of a function can be a built-in type (such as int or double), a class type or a composite type (such as int& or string*), or a void type, which means that the function does not return any values.

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

Second, the parameter transfer

Each time the function is called, all the parameters of the function are recreated, and the actual arguments passed will initialize the corresponding parameter.

The initialization of a formal parameter is the same as the initialization of a variable: if the parameter has a non-reference type, the value of the argument is copied, and if the parameter is a reference type, it is simply an alias of the argument.

2.1 Non-reference formal parameters

Normal non-potable type parameters are initialized by copying the corresponding arguments. When the taxiing parameter is initialized with an argument copy, the function does not have access to the arguments passed by the call itself, so the value of the argument is not modified.

A non-potable formal parameter represents a local copy of the corresponding argument. Modifications to this type of parameter only change the value of the local copy. Once the function execution is finished, the values of these local variables are gone.

2.1.1 Pointer parameters

The parameters of the function can be pointers, and the argument pointers are copied. As with other non-reference type parameters, any change to the class parameter only acts on the local copy. If the function assigns a new pointer to the parameter, the value of the argument pointer used by the keynote function does not change.

In fact, the copied pointer only affects the assignment of pointers. If a function parameter is a pointer to a non-const type, the function can be assigned by a pointer to modify the value of the object pointed to by the pointer.

You can initialize a pointer to a const object to point to a non-const object, but you cannot point a pointer to a non-const object to a const object.

2.1.2 Const formal parameter

When a function is called, if the function uses a non-potable non-const parameter, you can either pass a const argument to the function or pass a non-const argument.

2.1.3 Localization of duplicate arguments

Copying an argument does not fit in all cases, and it is not appropriate to copy the arguments, including:

1. When you need to modify the value of an argument in a function.

2. When a large object needs to be passed as an argument. For real-world applications, the time and storage costs of replicating objects are often too large.

3, when there is no way to achieve the replication of the object.

2.2 Reference Formal parameters

Reference parameters that do not need to be modified should be defined as const references. Ordinary non-const reference parameters are not very flexible to use. Such a parameter cannot be initialized with a const object, or by an expression argument that produces an rvalue or a literal value.

Iii. parameters of vectors and other container types

Normally, a function should not have a parameter for a vector or other standard library container type. Calling a function that contains a normal non-reference vector parameter will copy every element of the vector.

Four, array parameters

The array has two special properties that affect the functions we define and use on the array: first, you cannot copy an array, and when you use an array name, the array name is automatically converted to a pointer to its first element. A function that uses an array type parameter cannot be written because the array cannot be copied. Because arrays are automatically converted to pointers, functions that work with arrays typically manipulate arrays by manipulating pointers to elements in the array.

4.1 Definition of an array parameter

If you want to write a function that outputs the contents of an int array, you can specify the array parameters in the following three ways:

void Printvalue (int *) {}

void Printvalue (int []) {}

void Printvalue (int [10]) {}

Although an array cannot be passed directly, the formal parameters of the function can be written as an array.

In general, it is better to define an array parameter directly as a pointer than to use an array syntax definition. This makes it clear that the function manipulates a pointer to an array element, not the array itself. Because the array length is ignored, it is particularly misleading if the array length is included in the parameter definition.

When the compiler checks an argument associated with an array parameter, it checks that the argument is not the pointer, the type of the pointer, and the type of the array element, but does not check the length of the arrays.

4.2 Array Arguments

As with other types, an array parameter can be defined as either a reference or a non-reference type. In most cases, the array is passed as a normal non-reference type, when the array is quietly converted to a pointer. In general, a non-reference type's participation is initialized to a copy of its corresponding argument. When passing an array, the argument is a pointer to the first element of the array, and the parameter copies the value of the pointer, not the array element itself. The function manipulates a copy of the pointer, so the value of the argument pointer is not modified. However, the function can change the value of the array element it points to by using the pointer. Any change made by a pointer parameter modifies the array element itself.

C + + Primer Fourth Edition reading notes (vi) 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.