Just learn C + + that will, do the curriculum design will always go to the internet to find someone else to write a good program to refer to, then see the function parameters list of various complex types and strange writing on the head big, and then finally slowly make clear, here on the function of various types of parameters and functions of the return value for a summary.
1. Common Formal parameters
Passing a normal parameter is a value pass, passing a copy of the actual argument, and when the function is called, the parameter copies the argument, that is, the value of the parameter is the same as the argument, but the parameter has its own address in memory. When the function ends, the life cycle of the formal parameter terminates, and the operation inside the function does not affect the value of the argument. The classic value Exchange function code is as follows:
void Swap1 (int a, int b) {int temp; temp = A; A = b; b = temp;}
When the following statement calls the function, the values of I and J do not change, and the interchange fails because P and q are interchanged within the function, but do not affect I and J.
Swap1 (i, j);
2. Pointer parameters
As with normal parameters, pointer parameters are copies of pointer arguments, and actions in functions are limited to this copy, and changes to the pointer parameter itself do not affect the actual pointer, but you can modify the value of the object that the pointer points to by using a pointer parameter. The value Exchange function is still used as an example:
void swap2 (int *a, int *b) {int temp; temp = *a; *a = *b; *b = temp;}
When the function is called by the following statement, the values of I and J are interchanged successfully because the values of the addresses they refer to are exchanged by the pointer P and q, and I and P point to the same address, and J and Q point to the same address.
SWAP2 (&i, &j);
3. Reference formal parameters
As with all references, a reference parameter is directly associated with the object being bound to, not a copy of the object, so the operation of the parameter in the function is actually in the operation of the argument. The value exchange function of the reference parameter is as follows:
void swap3 (int &a, int &b) {int temp; temp = A; A = b; b = temp;}
When the following statement calls the function, the values of I and J are interchanged successfully because P and Q are only references to I and J, and the exchange of P and Q is actually the value of I and J in Exchange.
Swap1 (i, j);
4.const Formal parameters
The use of the Const qualifier allows the formal parameter to be called only within the function and cannot be changed, because the argument is still passed as a copy, so it can be either a const object or a non-const object passed to the const parameter.
When writing a function, if the sole purpose of using a reference parameter is to avoid copying the arguments, you should define the formal parameter as a const reference. Doing so can make calling functions more flexible. In this example, the function func () does not use a const parameter:
int func (int &val) {return ++val;}
The following calls are all wrong, because non-const reference parameters can only be associated with a completely homogeneous non-const parameter object, and the parameter of Func is changed to a const reference to solve these problems.
void Main () {const int v1 = 42; V2 = func (v1); Error: V2 is const V2 = func (0); Error: 0 is not an lvalue v2 = func (v1 + 2); Error: V1 + 2 is not a left value }
5. Array parameters
Because arrays cannot be copied, functions that work with arrays typically manipulate arrays by manipulating pointers to elements in the array, such as the following three ways:
void Showarray (int*) {}void showarray (int[]) {}void showarray (int[10]) {}
When the array size needs to be strictly specified, the reference to the array is passed, instead of a pointer to the array element, and the array itself is accessed by reference:
void Showarray (int (&arr) [10]) {}
6. Function return value
As long as a function that is not of type void has a return value, the normal type, such as int, and the type of the standard library type such as string as the return value are no longer discussed in detail, and the reference is returned here.
When a parameter is a reference, you can return a reference to the parameter:
Const string &shorterstring (const string &S1, const string &s2) { return s1,size () < S2.size ()? s1:s2 ;}
This function returns a reference to the shorter string in two string type parameters.
It is important to note that the reference returns an Lvalue, as in the following example, the object s bound by Str is the lvalue, but the reference to the Lvalue is returned, and the returned reference is placed to the left of the equal sign, which is the equivalent of placing s on the left side of the equals sign.
Char &get_val (String &str, String::size_typr ix) {return str[ix];} int main () {string S ("a value"); Get_val (s, 0) = ' A '; correct return 0;}
Finally, note that you should never return a reference to a local object in a function and a pointer to a local object.