The description of the C ++ parameter can be a constant value for a specified application. In general, it can be a variable, it is used to control other quantities that change as they change. In other words, the parameters are for our reference.
In this program, reference is used as the form parameter in the fun () function. The real parameter corresponding to the call should be an array name, and the reference here is to give an alias to the array. The operation on Array B in the fun () function is equivalent to the operation on Array a referenced by array B.
This call method is often used in C ++.
Instance:
- #include <iostream.h>
- 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<n-1; i++)
- b[7]+=b[i];
- }
-
- void main()
- {
- int m=8;
- fun(a, m);
- cout<<a[7]<<endl;
- }
Order of function parameters
When a function has multiple parameters, the C ++ language does not specify the order in which the C ++ parameters are evaluated during function calling. The compiler determines the order of values for real parameters based on the code optimization needs. Some compilers define the order from left to right, and some compilers define the order from right to left.
It does not affect general parameters. However, if the real parameter expression contains operators with side effects, it may lead to ambiguity due to different order of evaluation. For example, int z = add_int (++ x, x + y);. In this way, different compilers may produce different results.
Set the default value of the Parameter
In C ++, you can specify the default value for one or more parameters during function description or definition. However, to the right of a parameter with the default value specified, the parameter without the default value cannot be displayed. In the preceding description of the add_int () function, the default value is specified for the rightmost parameter of the function.
When a function is called, the compiler combines the real participation parameters in the order from left to right. When the number of real parameters is insufficient, the compiler uses the default values in the description or definition in the same order to supplement the missing real parameters. For example, if you have the following function call expressions:
When you specify a parameter, the default value is not only a numerical value, but also any complex expression. Using arrays as function parameters can be divided into the following three situations: (the results of the three cases are the same, but the calling mechanism is different)
- Mode description of the VC ++ Development Environment
- In-depth description of C ++ open source program history
- How to Learn C ++ applications correctly
- Detailed description of the C ++ program design structure
- Introduction to the design principles of C ++
1. Both form parameters and real parameters use arrays.
An array is used to call the real parameters of a function, and an array is used to call the form parameters of a function. This calling mechanism is to share the same array in the memory of the form parameters and the real parameters. Therefore, in the called function, the element value in the array is changed, because they share the same array.
2. Both the form parameter and the real parameter use the pointer of the corresponding array.
In the C ++ parameter, the array name is specified as a pointer, which is the pointer to the first element of the array, its value is the address value of the first element of the array. Therefore, the array name is a constant pointer. In reality, the form parameter and the real parameter use a pointer, And the other uses an array. When using pointers, you can use an array name or another defined pointer to an array.
3. Use an array name to reference a real Parameter
The following describes how to use the reference method for array types: first, define an int-type array type with the Type Definition Statement, as shown below: Then, use array to define arrays and references.
- Differences between standard input implementation methods in C and C ++
- How does the C ++ compiler allocate storage space for Const constants?
- Basic Conception and method of C ++ Class Library Design
- Several Methods for converting C ++ Language
- How to better compile C ++ code