C + + Learning diary--about value invocation and reference invocation

Source: Internet
Author: User

If you want the function to change the value of a variable, the corresponding parameter must refer to the calling argument. Therefore, you must add the "&" symbol after the parameter type. A value call parameter is just a local variable. When a set of data requires multiple functions to be processed, it is also called by reference.

So far we've been calling parameters using the value of the call, and the corresponding parameter in the function call can be a variable, but the function only uses the value of the variable and cannot change it in any way. When you use a value call, the value of the substitution parameter is the only argument.
For an input function, we want to replace the parameter with a variable (not a variable value). ---Reference invocation mechanism is used to solve this kind of problem.
With reference to the calling parameter, the corresponding argument in the function call must be a variable, and the real parametric will replace the formal parameter, as if it were a function body that assigns the real parametric verbatim to the function definition instead of the formal parameter. Executes the code of the function body, you can change the value of the argument variable.

  1. #include <iostream>
  2. Using namespace std;
  3. void Getnumer (int& input1, int& input2)
  4. {
  5. cout<<"Please input 2 integer" <<endl;
  6. cin>>input1>>input2;
  7. }
  8. void Swapvalues (int& var1, int& var2)
  9. {
  10. int temp;
  11. temp = var1;
  12. var1 = var2;
  13. VAR2 = temp;
  14. }
  15. void Shownunber (int output1, int output2)
  16. {
  17. cout<<"The swaped Numebrs now Show is:" <<endl;
  18. cout<<output1<<"" <<output2;
  19. cout<<endl;
  20. }
  21. int main ()
  22. {
  23. int fisrtval;
  24. int secondval;
  25. Getnumer (Fisrtval, secondval);
  26. Swapvalues (Fisrtval, secondval);
  27. Shownunber (Fisrtval, secondval);
  28. return 0;
  29. }

Program variables are treated as memory units, with one address for each memory unit.
When compiling the above program, the variables Firstval and secondval may be assigned to cell 1010 and unit 1012, the actual memory units are variables. When a function call is executed, it is not the argument name passed to the function, but a list of memory units corresponding to each name.

Firstval---> 1010--->input1

Secondval---> 1012--->input2


Regardless of what the function body does to the formal parameter, it is actually manipulating the variables in the memory unit. In this example, the command in the body of the GetNumber () function indicates that the

CIN has a value in INPUT1, which is equivalent to storing the value in a variable of memory unit 1010 (exactly the variable firstval). So no matter how the computer input1 and input2 into

What you do is actually the operation of the variables Firstval and secondval.

C + + Learning diary--about value invocation and reference invocation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.