Talking about the parameters of C ++ functions

xiaoxiao2021-03-06  61

Function parameters evaluation order

When a function has multiple parameters, the C language does not specify the order sequence of the actual parameters when the function call is called. Compilation

According to the need to optimize the code, you must specify the order sequence of the arguments. Some compilers specify from left to right,

Some compilers are specified from the right to left, which has no effect on the general parameters of different regulations. but

Yes, if an operator with side effects in the real-argument, it is possible to generate different order of evaluation.

Erlightening. For example: int z = add_int ( x, x y); then, it is possible to produce in different compilers.

Different results.

Set the default value of parameters

In the C language, one or more parameters are allowed to specify the default value when the function is explained or defined. However, the requirements are

A right side of the parameter specified by the default value cannot appear that the parameters do not specify the default value. E.g:

INT Add_int (int X, int 10);

In the description of the function add_int () described above, the default value is specified on one of the rightmost parameters of the function.

When the function is called, the compiler will participate in the combination from the left to right, when the number of the arguments is insufficient, compile

The lack of arguments will be complementaryly used in the same order in the same order. For example, if there is a letter

Number call expression:

Add_int (15)

It will call the expressions with the following:

Add_int (15, 10)

Is equivalent.

Specifying the default value to a parameter is not only a value, but also any complex expression.

Use the array for function parameters

The array function parameters can be divided into the following three cases: (the result of these three cases is the same, but the call used

Different mechanisms)

1. Use arrays and arguments

The array name of the active call function is called, the array of modified functions, this calling mechanism is a total ginseng and real parametric

Use the same array in memory. Therefore, in the modified function, it has changed a certain value in the array, and the call letter is called.

The element value of this array is also changed because they are shared with the same array.

2. Conversion and arguments use corresponding array pointers

In C , the array name is specified as a pointer, the pointer is a pointer to the first element of the array, the country is

Its value is the address value of the array header, so the array name is a constant pointer.

In practice, the conformation and the inner parameters are used in the use of pointers, and the other is also possible. You can use an array when using a pointer

Name, you can also use additional definition points to the pointer to the array.

3. Reference references for array names

How to use the reference mode for array types, here is the following description: First use the type definition statement to define an INT type

Array type, as shown below:

Typedef int Array [8];

Then, use array to define an array and reference.

Example:

#include

Typedef int Array [8];

INT A [8] = {1, 3, 5, 7, 9, 11, 13};

Void Fun (Array & B, INT N)

{

For (int i = 0; i

B [7] = B [I];

}

void main ()

{

INT m = 8;

Fun (a, m);

COUT << a [7] << endl;

}

In the program, in the FUN () function, the reference corrective reference should be an array when the reference is used.

Name, the reference here is to give arrays from individual names. The operation of the array b in the FUN () function is equivalent to B reference

转载请注明原文地址:https://www.9cbs.com/read-119439.html

New Post(0)