Evaluation Order of function arguments
When a function has multiple parameters, the C + + language does not specify the order in which the arguments are evaluated when the function is called. The compiler, based on the need to optimize the code, prescribes the order of evaluation for the arguments. Some compilers stipulate that from left to right, some compilers stipulate that from right to left, this kind of different rules for the order of Evaluation have no effect on the general parameters. However, if an operator with side effects is present in an argument expression, it is possible to produce two semantics due to a difference in the order of evaluation. For example: Intz = Add_int (++x,x+y), which makes it possible to produce different results in different compilers.
Set the default value for a parameter
In the C + + language, allows you to specify a default value for one or more parameters when a function is described or defined. However, a parameter that does not have a default value cannot appear on the right side of an argument that specifies a default value. For example:
Intadd_int (int x, int10);
In the description of the function add_int () above, a default value is specified for the rightmost parameter of the function.
When a function is called, the compiler binds the actual participating parameters in order from left to right, and when the number of arguments is insufficient, the compiler complements the missing arguments in the same order with the default values in the description or in the definition. For example, if you have the following function call expressions: Add_int (15)
It will be called with the following invocation expression:
Add_int (15,10)
is equivalent.
Specifying a default value for a parameter is not only a numeric value, but can also be any complex expression.
Using Arrays as function arguments
Array function parameters can be divided into the following three cases: (these three cases have the same result, but the invocation mechanism is different)
1. Both formal parameters and arguments are in arrays
The arguments to invoke the function are array names, the parameters of the called function are arrays, and the mechanism of the invocation is the same array of parameters in the shared memory of the parameter and the argument. Therefore, the value of an element that is not in the array is changed in the called function, and the values of the elements on the calling function are also changed because they are shared by the same array. 2. Parameters and arguments are both pointers to the corresponding array
In C + +, the array name is defined as a pointer to a pointer to the first element of the array whose value is the address value of the first element of the array, so the array name is a constant pointer.
In practice, formal parameters and arguments are one with pointers, and another with arrays. You can use an array name when using a pointer, or you can use a pointer to an array that you define differently.
3. Argument with array name parameter reference
How to use a reference method for an array type, here's how to define an int type of array type with a type definition statement, as follows:
Typedefint Array[8];
Then, use array to define the array and the 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<}
In this program, in the Fun () function, the reference is used as the parameter, and the argument corresponding to the call should be an array name, where the reference is to give the array an individual name. The operation of the array B in the fun () function is equivalent to the operation of arrays a referenced by B. In C + +, this type of invocation is common.