I have never been clear about what they are doing. This is a good summary:
1: pass by value:
The procedure is as follows:
# Include <iostream> <br/> using namespace STD; <br/> void swap (int A, int B) // pointer storage address <br/>{< br/> int C; <br/> cout <"before: A:" <A <"B: "<B <Endl; <br/> C = A; <br/> A = B; <br/> B = C; <br/> cout <"after: A:" <A <"B:" <B <Endl; <br/>}< br/> int main () <br/>{< br/> int A = 3, B = 4; <br/> cout <"before Main Program: A:" <A <"B:" <B <Endl; <br/> swap (, b); // get the address to pass the memory address of the variable <br/> cout <"after the main program: A:" <A <"B: "<B <Endl; <br/> return 0; <br/>}
The output result is:
Before the main program: A: 3 B: 4
Before: A: 3 B: 4
After: A: 4 B: 3
Before the main program: A: 3 B: 4
The value of the main program has not changed. The changed value is the copy value of A and B in the stack. When the value is transferred, copy a copy of the variable in the stack for operation, in this way, the previous values cannot be changed. be careful when using it.
2: Address-based transmission:
In this case, the address is used to pass the program as follows:
# Include <iostream> <br/> using namespace STD; <br/> void swap (int * a, int * B) // pointer storage address <br/>{< br/> int C; <br/> cout <"before: A:" <* A <"B: "<* B <Endl; <br/> C = * A; <br/> * A = * B; <br/> * B = C; <br/> cout <"after: A:" <* A <"B:" <* B <Endl; <br/>}< br/> int main () <br/>{< br/> int A = 3, B = 4; <br/> cout <"before Main Program: A:" <A <"B:" <B <Endl; <br/> swap (&, & B); // get the address to pass the memory address of the variable <br/> cout <"after the main program: A:" <A <"B: "<B <Endl; <br/> return 0; <br/>}< br/>
This is the parameter &, & B is used to get the address, so the address passed in is received by the pointer. Therefore, void swap (int * a, int * B) is available when defining the function) defines two pointers to store the address. In this way, we can understand that the operations in the SWAp () {} function are operated by pointers * a, * B
The result is:
Before the main program: A: 3 B: 4
Before: A: 3 B: 4
After: A: 4 B: 3
Before the main program: A: 4 B: 3
The result is changed because the memory address of A and B is operated directly.
3: pass by alias (reference:
# Include <iostream> <br/> using namespace STD; <br/> void swap (Int & A, Int & B) // receive two aliases <br/>{< br/> int C; <br/> cout <"Preface: A:" <A <"B: "<B <Endl; <br/> C = A; <br/> A = B; <br/> B = C; <br/> cout <"after: A:" <A <"B:" <B <Endl; <br/>}< br/> int main () <br/>{< br/> int A = 3, B = 4; <br/> cout <"before Main Program: A:" <A <"B:" <B <Endl; <br/> swap (, b); // when a B is passed directly, the alias is received by the alias. <br/> cout <"after the main program:: "<A <" B: "<B <Endl; <br/> return 0; <br/>}< br/>
In this case, the accepted parameters are the two aliases, which are the aliases of a B in the main function respectively. Because the aliases are the same as themselves, the results change.
Before the main program: A: 3 B: 4
Before: A: 3 B: 4
After: A: 4 B: 3
Before the main program: A: 4 B: 3
It can be seen that the purpose is not fulfilled when values are passed accidentally. It is difficult to write the code if the address is passed. If you are not careful, errors will occur, there are not so many issues to consider when using references for transmission, so we can use them with peace of mind. When writing a function, we can use references, so we don't have to worry too much about calling it.